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

How to change animation on touch?

Asked by 4 years ago
function OnTouch(hit)
game.Players.LocalPlayer.Character.Anima.run.WalkAnim.AnimationId = "http://www.roblox.com/asset/?id=4958225000"
end

script.Parent.Touched:connect(OnTouch)

Error: error: Workspace.Knight.Button.Animation:2: attempt to index nil with 'Character' (I asked the discord, I wanted to see if I can get a different answer here too)

0
all of that is in a script TheBuliderMC 84 — 4y
0
localscript? R_LabradorRetriever 198 — 4y
0
first you have to check if it is a character or not 123nabilben123 499 — 4y

1 answer

Log in to vote
1
Answered by
rexhawk 222 Moderation Voter
4 years ago

(This might not be fitting specifically for your case but this is some issues I saw)

First off, you're not checking if the thing that touched the part is even a character, so it might touch a random workspace object and attempt to find the "character" of that...

Second off, you don't need to do that long path to the character, you can just get the part's parent (after you've confirmed it is indeed a character)

function OnTouch(hit)
    local humanoid = hit:FindFirstChild("Humanoid")
    if humanoid then
        hit.Parent:WaitForChild("Anima").run.WalkAnim.AnimationId = "http://www.roblox.com/asset/?id=4958225000"
    end
end

script.Parent.Touched:Connect(OnTouch)

(also using :Connect instead of :connect is recommended)

Ad

Answer this question