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

Output error "attempting to index a nil value"?

Asked by 8 years ago

My script is:

local button = script.Parent

script.Parent.MouseButton1Up:connect(function()
if button.Text == ("Run") then  
    local h = game.Players:FindFirstChild("Humanoid")
    button.Text = ("Walk")
    h.Walkspeed = 16 * 1.5
elseif button.Text == ("Walk") then
    local h = game.Players:FindFirstChild("Humanoid")
    button.text = ("Run")
    h.Walkspeed = 16
end
end)

The complete output message:

Players.Player.PlayerGui.ScreenGui1.Frame.TextButton.Script:7: attempt to index local 'h' (a nil value) 15:01:54.335 - Stack Begin 15:01:54.337 - Script 'Players.Player.PlayerGui.ScreenGui1.Frame.TextButton.Script', Line 7

1 answer

Log in to vote
1
Answered by 8 years ago

Line 5 local h = game.Players:FindFirstChild("Humanoid") there is your error, you can't Find the Humanoid in the service Players.

You should Change the line 5 for local h = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")

LocalPlayer because its will take player by player.

Character because when you are in the LocalPlayer you can take the Character

Ad

Answer this question