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

Output error "attempting to index a nil value"?

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

✓ Accepted answer

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

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