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

Why is Character Nil?

Asked by
Chronomad 180
8 years ago

This script should be playing an animation based on the value within the script. But instead I get this error.

00:34:49.183 - Players.Player1.PlayerGui.PlayerInputManager:50: attempt to index field 'Character' (a nil value)

The script is inserted into the players gui when a guibutton is clicked. Thanks for any help.



local player = game:GetService('Players').LocalPlayer local already_ran = false local grounded = false local idleAnimation = Instance.new("Animation") idleAnimation.AnimationId = "rbxassetid://356344446" local Left = Instance.new("Animation") Left.AnimationId = "rbxassetid://356344643" local Right = Instance.new("Animation") Right.AnimationId = "rbxassetid://326922088" local Forward = Instance.new("Animation") Forward.AnimationId = "rbxassetid://312976450" local Backward = Instance.new("Animation") Backward.AnimationId = "rbxassetid://312976450" local Jump = Instance.new("Animation") Jump.AnimationId = "rbxassetid://326922088" function playAnimation(parent) local AnimTrack = player.Character.Humanoid:LoadAnimation(parent) AnimTrack:Stop() wait(.01) AnimTrack:Play() end function onGround(speed) if grounded == false then grounded = true end if speed > 1 then if already_ran == false then already_ran = true playAnimation(Left) end elseif speed > 0 and already_ran == true then already_ran = false playAnimation(idleAnimation) end end player.Character.Humanoid.Running:connect(onGround) function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.A then print("Left!!") script.Asset.Value = ("Left") elseif inputObject.KeyCode == Enum.KeyCode.D then script.Asset.Value = ("Right") elseif inputObject.KeyCode == Enum.KeyCode.W then script.Asset.Value = ("Forward") elseif inputObject.KeyCode == Enum.KeyCode.S then script.Asset.Value = ("Backward") end end game:GetService("UserInputService").InputBegan:connect(onKeyPress)
0
To use game.Players.LocalPlayer you would have to use a local script. User#11440 120 — 8y

2 answers

Log in to vote
1
Answered by 8 years ago

It could be the character hasn't loaded yet. Try putting this anywhere before line 18 (17 is most efficient)

repeat wait() until player.Character
0
Thanks! Chronomad 180 — 8y
Ad
Log in to vote
1
Answered by 8 years ago

You made a scripting error. From Line 4, to Line 17

Also, turn this into a local script if you are going to use LocalPlayer.

Here is a correct way to do this:

    local idleAnimation = Instance.new("Animation")
    idleAnimation.AnimationId = "rbxassetid://356344446"
    local Left = Instance.new("Animation")
    Left.AnimationId = "rbxassetid://356344643"
    local Right = Instance.new("Animation")
    Right.AnimationId = "rbxassetid://326922088"
    local Forward = Instance.new("Animation")
    Forward.AnimationId = "rbxassetid://312976450"
    local Backward = Instance.new("Animation")
    Backward.AnimationId = "rbxassetid://312976450"
    local Jump = Instance.new("Animation")
    Jump.AnimationId = "rbxassetid://326922088"
0
The script is local (Hence the word local written 10 times lol) And lines 4 - 17 was a copy and paste error. Updated the script. Chronomad 180 — 8y

Answer this question