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

Hey, how do I get character from player in a local script?

I am trying to make a press "e" to sprint script, but it says in the output: Players.rieleyhunt.Backpack.LocalScript:9: attempt to index upvalue 'character' (a nil value)

I am new to localscripts so please keep it easy for me

player = game.Players.LocalPlayer
playerMouse = player:GetMouse()
local character = player.Character


	playerMouse.KeyDown:Connect(function(key)
		
		if key == "e" then
			character.Humanoid.WalkSpeed = 50
		end
		
	end)

Thanks

1 answer

✓ Accepted answer
playerMouse = player:GetMouse()


    playerMouse.KeyDown:Connect(function(key)

        if key == "e" then
            game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 50
        end

    end)

Variables will save objects as the state they were in when they were saved. The problem is probably that the player object loads in before the character, since there's no character the value will be stored as "nil", basically nothing. To solve this i simply removed the "character" and "player" variables and instead used the current state of the character and player, the "raw" state if you could call it that.

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