Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you fix this animation script with a nil value error?

This code is inside a local script in a button, but I don't understand what attempt to index local 'plr' (a number value) means or how to fix it, Please help!

Script:

local Animation = script.Parent.Animation
local button = script.Parent

button.MouseButton1Down:connect(function(plr)
	wait(0.1)
	local animeTrack = plr.Humanoid:LoadAnimation(Animation)
	animeTrack:Play()
	wait(1.5)
	animeTrack:Pause()
end)

The error is on

local animeTrack = plr.Humanoid:LoadAnimation(Animation)

how can you fix this so that it will work and fix the error too?

If works/helped answer will be upvoted and accepted, thanks for trying or helped

1 answer

✓ Accepted answer

Since you are using a LocalScript, a function cannot access a players character. Instead, you can access the players character by using game.Players.LocalPlayer.Character.

Here is your fixed script.

local Animation = script.Parent.Animation
local button = script.Parent
local plr = game.Players.LocalPlayer --Access the Player.
local char = plr.Character --Access the players character.

button.MouseButton1Down:connect(function()
    wait(0.1)
    local animeTrack = char.Humanoid:LoadAnimation(Animation)
    animeTrack:Play()
    wait(1.5)
    animeTrack:Pause()
end)

2 comments

1

Thanks, upvoted and accpeted

0

You're welcome and thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.