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

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

Asked by 4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
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.

Ad

Answer this question