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?
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