Help on how to play an animation when pressing a part?

I’m a beginner to scripting Roblox Lua. I want to have an animation proceed when a player clicks on a part. For more information, the player clicks on a spill or dirty spot on the floor and has to sweep it up. Once swept, the NPC gives the player money. This is the animation: Sweeping Animation - Roblox

2 Likes

Insert a ClickDetector into the part, once it has been clicked, it will fire an event which you can use in a LocalScript or ServerScript (for this case you’ll want to use a ServerScript) and it can be picked up like so:

local ClickDetector = part.ClickDetector
local Animation = path.to.animation

ClickDetector.MouseClick:Connect(function(PlayerWhoClicked) -- Argument for the player who clicks the detector.
   local AnimationTrack = PlayerWhoClicked.Character:WaitForChild("Humanoid"):LoadAnimation(Animation) -- loading anim
   AnimationTrack:Play() -- playing anim, animations do not yield the thread so we can continue the code
end)

ClickDetectors also have some pretty neat features, you can also change the Cursor Icon once the cursor enters the part assigned to the ClickDetector and the MaxActivationDistance. There are also some other events which you can read in the wiki!

EDIT: Don’t forget to add the animation’s id into the Animation! (Set it with the property in the Animation called AnimationId)

3 Likes

Assuming there is a ClickDetector inside the part, you need to retrieve the player character from the person who clicked, search for the humanoid, and load the animation. To do this, make sure there is a click detector in the part, and insert this script.

local part = script.Parent
local clickDetector = part:WaitForChild("ClickDetector")
local ID = "http://www.roblox.com/asset/?id=5100340522"
local animation = Instance.new("Animation")
animation.AnimationId = ID
clickDetector.MouseClick:Connect(function(player)
    local character = player.Character or player.CharacterAdded:Wait()
    if character and character:FindFirstChild("Humanoid") and character.Humanoid.Health > 0 then
        character.Humanoid:LoadAnimation(animation):Play()
    end
end)
8 Likes

So, inside of the part I need to put a Click Detector and insert that script inside of it?

1 Like

With how he wrote the script, the script goes inside that part, not the click detector.

2 Likes

Can I add you guys incase of a near future problem I have. I’ve just started scripting in Roblox Lua.

1 Like

Also for future reference, please try not to split your question into two topics, especially when posted so close together. If you wait, someone in this community would sure to have given you a sufficient answer.

Other Post

1 Like

Sure, I’d be fine helping you along.

2 Likes

Thank you very, very much. I’ve looked at millions of tutorials and so many things, but I still get really confused. I’ve just sent you a friend request as well.

1 Like

What do you mean by it being split into two pieces?

1 Like

As of right now I have yet to receive the request. Also it would be easier if you added me on discord Wizard#4930 and we simply proceeded with any questions there.

2 Likes

Yeah, sure. My discord is: MiniGamet#0003

1 Like

Do I put the server script inside of the ‘ServerScriptService’?

1 Like

It doesn’t really matter where the server script is kept in b_oolean’s script, however server scripts in general only run if they are kept in certain places. Serverscriptservice is one of those places, so it would work in serverscriptservice.

2 Likes

Thank you very, very much. Can I add you as well so that if I have any problems with scripting, again?

2 Likes

Yeah, sure.

2 Likes

How do I replace the ‘path’ text with the animations URL? Or is that even how you do it?

1 Like

Also, how do I replace the ‘path’ text with the animations URL? Or is that even how you do it?

1 Like

I think he was assuming you already made an animation instance and set the animationId property to your Id. So what you’d do is create a new animation instance, set the animationId property to your id, and make a path to it.

1 Like

by path.to.animation I generally mean the object path to the Animation Object
so say for example you insert a new Animation Object in ServerStorage and you name it “myAnimation”, the path.to.animation would then become game.ServerStorage.myAnimation

2 Likes