Tool=script.Parent Handle=Tool:WaitForChild("\072\097\110\100\108\101")Players=game:GetService("\080\108\097\121\101\114\115")Debris=game:GetService("\068\101\098\114\105\115")UserInputService=game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")Folders=Tool:WaitForChild("\070\111\108\100\101\114\115")RemotesFolder=Folders:WaitForChild("\082\101\109\111\116\101\115")ServerControl=RemotesFolder:WaitForChild("\083\101\114\118\101\114\067\111\110\116\114\111\108")ClientControl=RemotesFolder:WaitForChild("\067\108\105\101\110\116\067\111\110\116\114\111\108")Animations={}ToolEquipped=false function SetAnimation(W,h)if W=="\080\108\097\121\065\110\105\109\097\116\105\111\110"and(h and(ToolEquipped and Humanoid))then for W,U in pairs(Animations)do if U.Animation==h.Animation then U.AnimationTrack:Stop()table.remove(Animations,W)end end local W=Humanoid:LoadAnimation(h.Animation)table.insert(Animations,{Animation=h.Animation;AnimationTrack=W})W:Play(h.FadeTime,h.Weight,h.Speed)elseif W=="\083\116\111\112\065\110\105\109\097\116\105\111\110"and h then for W,U in pairs(Animations)do if U.Animation==h.Animation then U.AnimationTrack:Stop()table.remove(Animations,W)end end end end function CheckIfAlive()return(Character and(Character.Parent and(Humanoid and(Humanoid.Parent and(Humanoid.Health>0 and(Player and Player.Parent))))))and true or false end function Equipped(W)Character=Tool.Parent Player=Players:GetPlayerFromCharacter(Character)Humanoid=Character:FindFirstChild("\072\117\109\097\110\111\105\100")ToolEquipped=true if not CheckIfAlive()then return end PlayerMouse=W W.Button1Down:connect(function()InvokeServer("\077\111\117\115\101\067\108\105\099\107",{Down=true})end)W.Button1Up:connect(function()InvokeServer("\077\111\117\115\101\067\108\105\099\107",{Down=false})end)for W,h in pairs({KeyDown,KeyUp})do if h then h:disconnect()end end KeyDown=UserInputService.InputBegan:connect(function(W)local h=W.UserInputType local U=W.KeyCode if h==Enum.UserInputType.Keyboard then InvokeServer("\075\101\121\080\114\101\115\115\101\100",{Key=U.Name,Down=true})end end)KeyUp=UserInputService.InputEnded:connect(function(W)local h=W.UserInputType local U=W.KeyCode if h==Enum.UserInputType.Keyboard then InvokeServer("\075\101\121\080\114\101\115\115\101\100",{Key=U.Name;Down=false})end end)end function Unequipped()for W,h in pairs({KeyDown,KeyUp})do if h then h:disconnect()end end for W,h in pairs(Animations)do if h and h.AnimationTrack then h.AnimationTrack:Stop()end end Animations={}ToolEquipped=false end function InvokeServer(W,h)local U pcall(function()U=ServerControl:InvokeServer(W,h)end)return U end function OnClientInvoke(W,h)if W=="\080\108\097\121\065\110\105\109\097\116\105\111\110"and(h and(ToolEquipped and Humanoid))then SetAnimation("\080\108\097\121\065\110\105\109\097\116\105\111\110",h)elseif W=="\083\116\111\112\065\110\105\109\097\116\105\111\110"and h then SetAnimation("\083\116\111\112\065\110\105\109\097\116\105\111\110",h)elseif W=="\080\108\097\121\083\111\117\110\100"and h then h:Play()elseif W=="\083\116\111\112\083\111\117\110\100"and h then h:Stop()elseif W=="\077\111\117\115\101\080\111\115\105\116\105\111\110"then return PlayerMouse and{Position=PlayerMouse.Hit.p;Target=PlayerMouse.Target}or nil end end ClientControl.OnClientInvoke=OnClientInvoke Tool.Equipped:connect(Equipped)Tool.Unequipped:connect(Unequipped)


Tool = script.Parent
Handle = Tool:WaitForChild("Handle")

Players = game:GetService("Players")
Debris = game:GetService("Debris")
UserInputService = game:GetService("UserInputService")

Folders = Tool:WaitForChild("Folders")
RemotesFolder = Folders:WaitForChild("Remotes")
ServerControl = RemotesFolder:WaitForChild("ServerControl")
ClientControl = RemotesFolder:WaitForChild("ClientControl")

Animations = {}

ToolEquipped = false

function SetAnimation(mode, value)
	if mode == "PlayAnimation" and value and ToolEquipped and Humanoid then
		for i, v in pairs(Animations) do
			if v.Animation == value.Animation then
				v.AnimationTrack:Stop()
				table.remove(Animations, i)
			end
		end
		local AnimationTrack = Humanoid:LoadAnimation(value.Animation)
		table.insert(Animations, {Animation = value.Animation, AnimationTrack = AnimationTrack})
		AnimationTrack:Play(value.FadeTime, value.Weight, value.Speed)
	elseif mode == "StopAnimation" and value then
		for i, v in pairs(Animations) do
			if v.Animation == value.Animation then
				v.AnimationTrack:Stop()
				table.remove(Animations, i)
			end
		end
	end
end

function CheckIfAlive()
	return (((Character and Character.Parent and Humanoid and Humanoid.Parent and Humanoid.Health > 0 and Player and Player.Parent) and true) or false)
end

function Equipped(Mouse)
	Character = Tool.Parent
	Player = Players:GetPlayerFromCharacter(Character)
	Humanoid = Character:FindFirstChild("Humanoid")
	ToolEquipped = true
	if not CheckIfAlive() then
		return
	end
	PlayerMouse = Mouse
	Mouse.Button1Down:connect(function()
		InvokeServer("MouseClick", {Down = true})
	end)
	Mouse.Button1Up:connect(function()
		InvokeServer("MouseClick", {Down = false})
	end)
	for i, v in pairs({KeyDown, KeyUp}) do
		if v then
			v:disconnect()
		end
	end
	KeyDown = UserInputService.InputBegan:connect(function(InputObject)
		local InputType = InputObject.UserInputType
		local KeyCode = InputObject.KeyCode
		if InputType == Enum.UserInputType.Keyboard then
			InvokeServer("KeyPressed", {Key = KeyCode.Name, Down = true})
		end
	end)
	KeyUp = UserInputService.InputEnded:connect(function(InputObject)
		local InputType = InputObject.UserInputType
		local KeyCode = InputObject.KeyCode
		if InputType == Enum.UserInputType.Keyboard then
			InvokeServer("KeyPressed", {Key = KeyCode.Name, Down = false})
		end
	end)
end

function Unequipped()
	for i, v in pairs({KeyDown, KeyUp}) do
		if v then
			v:disconnect()
		end
	end
	for i, v in pairs(Animations) do
		if v and v.AnimationTrack then
			v.AnimationTrack:Stop()
		end
	end
	Animations = {}
	ToolEquipped = false
end

function InvokeServer(mode, value)
	local ServerReturn
	pcall(function()
		ServerReturn = ServerControl:InvokeServer(mode, value)
	end)
	return ServerReturn
end

function OnClientInvoke(mode, value)
	if mode == "PlayAnimation" and value and ToolEquipped and Humanoid then
		SetAnimation("PlayAnimation", value)
	elseif mode == "StopAnimation" and value then
		SetAnimation("StopAnimation", value)
	elseif mode == "PlaySound" and value then
		value:Play()
	elseif mode == "StopSound" and value then
		value:Stop()
	elseif mode == "MousePosition" then
		return ((PlayerMouse and {Position = PlayerMouse.Hit.p, Target = PlayerMouse.Target}) or nil)
	end
end

ClientControl.OnClientInvoke = OnClientInvoke

Tool.Equipped:connect(Equipped)
Tool.Unequipped:connect(Unequipped)
