Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a player toggle an animation when they click a brick (Clickdetector)?

I want the player to start an animation I have made when they click a brick. I have filtering enabled turned on. Here is the LocalScript for them to send the event to start the event/animation

script.Parent.tool.detector.ClickDetector.MouseClick:Connect(function()
	print("local script")
	script.Parent.animate:FireServer(2435453180)
	print("local script")
end)

This is the script that receives the event

script.Parent.animate.OnServerEvent:Connect(function(player,animationID)
	local animation = Instance.new("Animation")
	animation.AnimationId = "http://www.roblox.com/Asset?ID="..animationID
	
	local loadedAnimation = game.Workspace[player.Name].Humanoid:LoadAnimation(animation)
	loadedAnimation:Play()
	print("Server script")
end)

When I hit play and click the button it does nothing, and the output says nothing.

1 answer

✓ Accepted answer

Playing animations on a player should be done on a local script. If you want to play an animation on NPC (Non-player controlled) then play the animation using a server script.

See more of that here. Look at the bit on "Should I load on Server or Client"

You have the right idea just don't use a server script at all.

In Local Script:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local humanoid = character:WaitForChild("Humanoid")

local animationID = 2435453180

local animation = Instance.new("Animation")
animation.Parent = humanoid
animation.AnimationId = "http://www.roblox.com/Asset?ID="..animationID

local part = workspace:WaitForChild("Part")
local clickDetector = part:WaitForChild("ClickDetector")

clickDetector.MouseClick:Connect(function()
	--I believe you can load the animation outside of this function too
	local animTrack = humanoid:LoadAnimation(animation)
	animTrack:Play()
end)	

Let me know if you need an explanation for this. It really just is not having to use a server script or RemoteEvents when you don't need to.

edit; property fix & format

9 comments

0

So I put this in the local script (Which is in the StarterGUI) and in the output it says it ends on line 3?

0

Oh and also line 3 is the local humanoid = character:WaitforChild("Humanoid"), i just removed a line

0

For some reason I had put tool thinking you were working with one. Updated the code to find the part in workspace. Try again. Let me know if that error shows.

0

Also from looking at your old code. Make sure that ClickDetector's parent is the part in workspace.

0

Okay i've made the changes and now the script ends on Line 8. And do you mean that the variable 'tool' has to equal to the part that contains the ClickDetector? If so I believe i've made that correctly

0

Line 8 for me is: local tool = script.Parent.Parent.Workspace.gen1.tool.detector

0

You should have my code pasted in a local script inside of StarterCharacterScripts. Then have a part in workspace with a clickdetector inside it.

0

Okay

0

THANKS YOU SO MUCH, it works

This discussion is preserved as it was. New answers, comments, and votes are closed.