So I was making a shotgun weapon when I came across 2 errors. I was trying to load an animation from a LocalScript. The animation objects were inside the tool itself. The first error I came across was: "LoadAnimation requires the Humanoid object (Player1.Humanoid) to be a descendant of the game object" This is when I tested the game in solo mode on studio. The second error I got on a server was: "Players.Cowation.Backpack.Shotgun.Shotgun:5: attempt to index local 'character' (a nil value)" Clearly, the Character object is not a nil value. Apparently when trying to call to it, the script just outputs an error. The following is a bunch of code segments that I pulled from the LocalScript regarding the error. Here is an image of the layout of the tool.
local tool = script.Parent local character = game.Players.localPlayer.Character local humanoid = character.Humanoid local idle = tool.Idle -- Anim Object local activate = tool.Activate -- Anim Object local animTrackIdle = humanoid:LoadAnimation(idle) local animTrackActivate = humanoid:LoadAnimation(activate) local function onAnimationEquip() animTrackIdle:Play() end local function onAnimationActivate() animTrackActivate:Play() end tool.Equipped:connect(onAnimationEquip) tool.Activated:connect(onAnimationActivate)
Please help, I've seen others have this problem but none of the answers that I have seen were really clear to me.
if nobody answers that's fine
Well the error "Players.Cowation.Backpack.Shotgun.Shotgun:5: attempt to index local 'character' (a nil value)"
says Character is a nil value. And it Is nil because by looking at your script in the second line;
local character = game.Players.localPlayer.Character
Your forgot to cap the L in LocalPlayer
Also, theres more to it.
If your testing this in the actual game then you have to use WaitForChild()
because objects actually take time to load in so the game or studio may not have found Character yet.
so I suggest you do one of these;
local tool = script.Parent wait(1) local character = game.Players.LocalPlayer.Character -- local player is fixed ;) local humanoid = character.Humanoid local idle = tool.Idle -- Anim Object local activate = tool.Activate -- Anim Object local animTrackIdle = humanoid:LoadAnimation(idle) local animTrackActivate = humanoid:LoadAnimation(activate) local function onAnimationEquip() animTrackIdle:Play() end local function onAnimationActivate() animTrackActivate:Play() end tool.Equipped:connect(onAnimationEquip) tool.Activated:connect(onAnimationActivate)
that wait is enough time for the objects to load in
or;
local tool = script.Parent local character = game.Players.LocalPlayer:WaitForChild('Character') -- wait for character to load local humanoid = character.Humanoid local idle = tool.Idle -- Anim Object local activate = tool.Activate -- Anim Object local animTrackIdle = humanoid:LoadAnimation(idle) local animTrackActivate = humanoid:LoadAnimation(activate) local function onAnimationEquip() animTrackIdle:Play() end local function onAnimationActivate() animTrackActivate:Play() end tool.Equipped:connect(onAnimationEquip) tool.Activated:connect(onAnimationActivate)
Those can fix your problems, also, Roblox have their own events so you can do tool.Activated:Connect(function()
from the top instead of calling a function, that can improve your script.
Last thing is don't use connect
it's deprecated, meaning it can be removed use Connect
You can also use CharacterAdded
which is the best way
so it would be like this;
local tool = script.Parent local character = game.Players.LocalPlayer.CharacterAdded:Wait() -- wait for character to load local humanoid = character.Humanoid local idle = tool.Idle -- Anim Object local activate = tool.Activate -- Anim Object local animTrackIdle = humanoid:LoadAnimation(idle) local animTrackActivate = humanoid:LoadAnimation(activate) local function onAnimationEquip() animTrackIdle:Play() end local function onAnimationActivate() animTrackActivate:Play() end tool.Equipped:connect(onAnimationEquip) tool.Activated:connect(onAnimationActivate)
Fix your other little problems and your good to go!
Have fun and good luck developing!
If this helped then please accept answer ;)