I’m trying to make it where when a part is touched by a player, the player does an animation. In this case when a player touches a piece of food I want them to do an eating animation. I have a script in ServerScriptService with an animation attached to it. I want too make it where whenever you touch a part in workspace, the script in ServerScriptService will activate and play the animation. I was wondering if this is possible or if it isn’t. Thanks
Try this code out:
local part = workspace.Food
local debounce = false
part.Touched:Connect(function(toucher)
if toucher.Parent:FindFirstChildWhichIsA("Humanoid") then
local hum = toucher.Parent:FindFirstChildWhichIsA("Humanoid")
local animator = hum:WaitForChild("Animator")
if animator then
if debounce == true then
return
end
debounce = true
local anim = Instance.new("Animation") do
anim.AnimationId = "rbxassetid://9434761803"
end
local load = animator:LoadAnimation(anim) do
load:Play()
load.Stopped:Connect(function()
debounce = false
end)
end
end
end
end)
It works thank you but the animation doesn’t seem to ever stop.
Also is there a way to link the script to parts inside a model? So whenever the player touches the parts inside the model the animation plays?
Thanks for your help
for i, v in pairs(food:GetChildren()) do
v.Touched:Connect(function()
--stuff
end)
end
Where is that supposed to go in the script?
It replaces this function down here… I added a loop to loop through all the parts
Oh thanks it does work but the animation never stops playing which is the only problem I have at the moment.
Add this in the code:
load.Stopped:Connect(function()
task.wait(3)
debounce = false
end)
Here is what I’m using right now but the animation is still not stopping after 3 seconds.
local part = workspace.Food
local debounce = false
for i, v in pairs(part:GetChildren()) do
v.Touched:Connect(function(toucher)
if toucher.Parent:FindFirstChildWhichIsA("Humanoid") then
local hum = toucher.Parent:FindFirstChildWhichIsA("Humanoid")
local animator = hum:WaitForChild("Animator")
if animator then
if debounce == true then
return
end
debounce = true
local anim = Instance.new("Animation") do
anim.AnimationId = "rbxassetid://9890262335"
end
local load = animator:LoadAnimation(anim) do
load:Play()
load.Stopped:Connect(function()
task.wait(3)
debounce = false
end)
end
end
end
end)
end
Is the animation looped? You need to explicitly call the :Stop()
method on/through it in order for the animation to stop.
https://developer.roblox.com/en-us/api-reference/function/AnimationTrack/Stop
I tried making it stop by using “anim:Stop()” but it just kept playing the animation.
Because you’re creating a new animation object and thus a new animation track each time the part’s ‘Touched’ signal fires, create 1 of each.