Detecting RightClick and play an animation

Basically I am trying to make it so when RightClick is being held then an animation plays.

Its a server script and its in a tool.

The only thing that happens is it prints “equip” then stops, no errors.

local LightSaber = script.Parent
local player = LightSaber.Parent.Parent
local Character = player.Character
local humanoid = Character.Humanoid 
local hold -- if player is holding right click
local equipped
local UserInputService = game:GetService("UserInputService")
local Block1 = humanoid:LoadAnimation(script:WaitForChild('Block1'))
local Block2 = humanoid:LoadAnimation(script:WaitForChild('Block2'))
local Block3 = humanoid:LoadAnimation(script:WaitForChild('Block3'))
Block1.Priority = Enum.AnimationPriority.Action
Block2.Priority = Enum.AnimationPriority.Action4
Block3.Priority = Enum.AnimationPriority.Action
equipped = false

script.Parent.Equipped:Connect(function()
	print("equip") -- Only thing printed
	equipped = true
end)
script.Parent.Unequipped:Connect(function()
	equipped = false
end)

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if (gameProcessedEvent) then
		return
	end
	if (input.UserInputType == Enum.UserInputType.MouseButton2) then
		if equipped == true then
			print("hold is true and equip") -- Never prints
			hold = true
		end
	end
end)


UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if (gameProcessedEvent) then
		return
	end
	if (input.UserInputType == Enum.UserInputType.MouseButton2) then
		if equipped == true then
			print("hold is false and equip") -- Never prints
			hold = false
		end
	end
end)

while wait() do
	if hold and equipped == true then
		Block1:Play()
		wait(0.15)
		if hold and equipped == true then
			Block2:Play()
			wait(0.15)
			if hold and equipped == true then
				Block3:Play()
				wait(0.15)
				return
			end
		end
	else
		Block1:Stop()
		Block2:Stop()
		Block3:Stop()
	end
end

I really don’t know why this is happening, pretty sure I am using UserInputService correctly but maybe not?

local UserInputService = game:GetService("UserInputService")

Only works as client side.

it their a way to make it work for serverside, or should i just use remotes

I would not recommend using Remotes for this use case because that would be unnecessarily using up bandwidth for a problem that could be solved in a much simpler way.


Instead, you could create an Animator object within the Character model on the server side so that any Animations loaded through that object will be replicated to other players in the game.

*Edit: By default, there’s an Animator object in the Character’s Humanoid that should work for this if you don’t want to create one manually. I completely forgot about this when I first posted my reply!


This means that the code in the original post would be able to run entirely on the client-side via a LocalScript. After referencing the Animator object that was created by the server, use Animator:LoadAnimation() with the Animations at the top of the script:

-- Original code
local Block1 = humanoid:LoadAnimation(script:WaitForChild('Block1'))
local Block2 = humanoid:LoadAnimation(script:WaitForChild('Block2'))
local Block3 = humanoid:LoadAnimation(script:WaitForChild('Block3'))

-- New code
local AnimatorObject = humanoid:WaitForChild("Animator") -- Or if you create it somewhere else in the Character model, make sure to update this to reflect where it's placed
local Block1 = AnimatorObject:LoadAnimation(script:WaitForChild('Block1'))
local Block2 = AnimatorObject:LoadAnimation(script:WaitForChild('Block2'))
local Block3 = AnimatorObject:LoadAnimation(script:WaitForChild('Block3'))

This way, you’d be able to use the UserInputService to decide when to play those animations, all without needing to communicate between the client and server via Remotes.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.