local stats = script.Parent.Parent:WaitForChild("statistics") print("stats") local ability = game.Players.LocalPlayer:WaitForChild("ability") print("ability") local replicatedstorage = game:GetService("ReplicatedStorage") print("replicatedstorage") local statustag = replicatedstorage:WaitForChild("statustag") print("statustag") local player = game.Players.LocalPlayer local character = player.Character local humanoid = character.Humanoid print("humanoid") local using = Instance.new("BoolValue") using.Parent = game.Players.LocalPlayer using.Name = "using" using.Value = false ability.Value = "fast" function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.E and using.Value == false and ability.Value == "fast" and statustag.Value == "Game in progress" then using.Value = true print("E pressed.") repeat wait() until humanoid ~= nil script.Sound:Play() humanoid.WalkSpeed = humanoid.WalkSpeed + 20 wait(5) humanoid.WalkSpeed = humanoid.WalkSpeed - 20 using.Value = false end end game:GetService("UserInputService").InputBegan:connect(onKeyPress)
When the player presses 'e,' their walkspeed is supposed to go up by 20.
I get the following error: Line 11: Attempt to index local 'character,' a nil value.
I don't quite understand how it is nil, as I defined it above.
Sometimes the script will run before the Character is loaded. This makes your character
variable equal nil
. To get around this, you must use one of the many ways to wait for the character to load until defining it, my favorite being:
local plr = game.Players.LocalPlayer repeat wait() until plr.Character local chr = plr.Character
Best to use this instead of a repeat
local plr = game.Players.LocalPlayer plr.Character:WaitForChild('Humanoid') local chr = plr.Character --spyspace12