Greetings,
A few days ago I decided to make a Halloween game for my boss, I managed to build the houses and the rest and I paid someone to make some trick or treat animations, my only problem is that the animation can’t be played. I tried the script below and there is an error in the output. I don’t understand what is wrong. I need someone to help me fix this and make the animation play. Thanks!
function onTouched(hit)
local human = hit.Parent:findFirstChild("Humanoid")
if human then
local anim = human:LoadAnimation(script.Parent.Animation)
anim:Play()
end
end
script.Parent.Touched:connect(onTouched)
An animator is a child of the humanoid, you should check for that first.
-- Finds the humanoid
local humanoid = char:FindFirstChildOfClass("Humanoid")
-- Finds the animator
local animator = humanoid:FindFirstChildOfClass("Animator")
-- Checks whether the animator exists
if animator == true then
-- Declares a new variable,where we'll load the animation
local anim = animator:LoadAnimation(your_animation)
-- Plays the animation
anim:Play()
end
Hope that clarifies? A better example can be found via the link I posted, at the bottom of the page.
I have just 2 more questions, in the (your_animation) I need to put the ID of the animation or what?
And when I copied this code and I put it in the script the char appears with a underline. What should I do?
local animation = script.Animation
-- Finds the humanoid
local humanoid = char:FindFirstChildOfClass("Humanoid")
-- Finds the animator
local animator = humanoid:FindFirstChildOfClass("Animator")
-- Checks whether the animator exists
if animator == true then
-- Declares a new variable,where we'll load the animation
local anim = animator:LoadAnimation(animation)
-- Plays the animation
anim:Play()
end
-- Declares a function
local function onTouched(partTouched)
-- Defines a variable pointing to your animation
local animation = script.Animation
-- Finds the humanoid, fails: exits the function
local humanoid = partTouched.Parent:FindFirstChildOfClass("Humanoid") or return
-- Finds the animator, fails: exits the function
local animator = humanoid:FindFirstChildOfClass("Animator") or return
-- Loads the animation
local anim = animator:LoadAnimation(animation)
-- Plays the animation
anim:Play()
end
-- Calls the above function when the player touches the part
script.Parent.Touched:Connect(onTouched)
Animation is not a valid member of Workspace.Animation, because it is a valid member of Workspace.Animation["Animations Script"]. Change the local anim = human:LoadAnimation(script.Parent.Animation) to local anim = human:LoadAnimation(script.Animation).