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

Animation script error: Attempt to index field 'LocalPlayer' (nil value)?

Asked by 7 years ago

Basically, I'm trying to make a script where the part is touched, an animation is played in the player. This is my code:

local s = script.Parent
local animation = s.Eating_anim
local Player = game.Players.LocalPlayer:WaitForChild("Character")
local animTrack = Player.Humanoid:LoadAnimation(animation)

script.Parent.ClickDetector.MouseClick:connect(function()
    animTrack:Play()
end)

Apparently there is an error in line 3: Workspace.Part.Script> :3: attempt to index field 'LocalPlayer' (a nil value)>

Anyone knows what this means and how to solve the error?

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

It seems like 'LocalPlayer' is nil because the script type is not correct.

A possible solution would be this:


script.Parent.ClickDetector.MouseClick:connect(function(Player) -- Click Detectors also give the player that clicked. local Character = Player.Character -- We get the player's character local s = script.Parent -- We reference the parent of this script local animation = s.Eating_anim -- We get the Animation local animTrack = Character.Humanoid:LoadAnimation(animation) -- We load the animation end)

These links can be useful to you for any further references:

http://wiki.roblox.com/index.php?title=API:Class/Player http://wiki.roblox.com/index.php?title=API:Class/ClickDetector http://wiki.roblox.com/index.php?title=API:Class/Script http://wiki.roblox.com/index.php?title=API:Class/LocalScript http://wiki.roblox.com/index.php?title=API:Class/Animator/LoadAnimation http://wiki.roblox.com/index.php?title=API:Class/Humanoid/LoadAnimation

Some info:

LocalPlayer is nil on scripts, using a LocalScript would be better because, on them, the 'LocalPlayer' property is not nil.

-- LocalScript
print(game.Players.LocalPlayer.Name) --> "Your Username"
-- Script
print(game.Players.LocalPlayer.Name) --> "Attempt to index field 'LocalPlayer' (nil value)"

Hope this helped. (Sorry for any mistakes, grammar mistakes or bad explanation. Use the wiki or search on YouTube for more info.)

Edit: If the animation still not play, try this:

Script(FIX):

script.Parent.ClickDetector.MouseClick:connect(function(Player) -- Click Detectors also give the player that clicked.
    local Character = Player.Character -- We get the player's character
    local s = script.Parent -- We reference the parent of this script
    local New = script.AnimLoader:Clone()
New.Parent = Player.Character
end)

LocalScript:

local Animation = Instance.new("Animation", game.Players.LocalPlayer.Character) -- We make a animation on the character
Animation.AnimationId = 0 -- Animation ID goes here
local Anim = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(Animation)
wait(.1) -- A delay of 0.1 Secs
Anim.Priority = Enum.AnimationPriority.Core

-AlessandroG200

0
Thanks. There were no errors but the animation isn't being played. Do you know why? Florian27 76 — 7y
0
I have done an edit, you can try the edits that i made. AlessandroG200 77 — 7y
0
It says the animation fails to load. The animation is mine and I have used everything you wrote. Florian27 76 — 7y
0
Animation "1293869333" failed to load in "Workspace.Florian27.Animation.AnimationId": Animation failed to load Florian27 76 — 7y
0
For me it worked. It may be a bug on Roblox's server/Roblox Studio. I will try to take a look on the script. Use YouTube tutorials to try to solve the problem. AlessandroG200 77 — 7y
Ad

Answer this question