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

Why is Character Nil?

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)





1 comment

0

To use game.Players.LocalPlayer you would have to use a local script.

2 answers

✓ Accepted answer

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

1 comment

0

Thanks!

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"

1 comment

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.

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