Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 5 years ago
Edited 5 years ago

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

Log in to vote
0
Answered by
xPolarium 1388 Moderation Voter
5 years ago
Edited 5 years ago

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

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? Marty999 82 — 5y
0
Oh and also line 3 is the local humanoid = character:WaitforChild("Humanoid"), i just removed a line Marty999 82 — 5y
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. xPolarium 1388 — 5y
0
Also from looking at your old code. Make sure that ClickDetector's parent is the part in workspace. xPolarium 1388 — 5y
View all comments (5 more)
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 Marty999 82 — 5y
0
Line 8 for me is: local tool = script.Parent.Parent.Workspace.gen1.tool.detector Marty999 82 — 5y
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. xPolarium 1388 — 5y
0
Okay Marty999 82 — 5y
0
THANKS YOU SO MUCH, it works Marty999 82 — 5y
Ad

Answer this question