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

Waitforchild not working?

game.Players.PlayerAdded:connect(function(p)
	local leaderstat = Instance.new("Model")
	leaderstat.Name = "leaderstat"
	leaderstat.Parent = p
		local c = Instance.new("StringValue")
	c.Name = "coords"
	c.Value = 2
	c.Parent = leaderstat
end)


game.Players.PlayerAdded:connect(function(p)
	local leaderstats = Instance.new("Model")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = p
	local hunger = Instance.new("NumberValue")
	hunger.Name = "Hunger"
	hunger.Value = 100
	hunger.Parent = leaderstats
	local thirst = Instance.new("NumberValue")
	thirst.Name = "thirst"
	thirst.Value = 100
	thirst.Parent = leaderstats
end)


print("here here")
player = game.Players.LocalPlayer
print("code blue")
local e = false

if player.Hunger.Value <= 10 then
	e = true
	while e do
		wait(2)
	player.Character.Humanoid.Health = player.Character.Humanoid.Health - 10
	end
end


if player.Character.Humanoid.Health <= 0 then
	e = false
end

basically, this is for a hunger stat. it does the leaderstats part perfectly, but it doesnt print code blue. it doees print here here, which is why i know the waitforchild is messing up. am i using it wrong? or is it something else?

3 comments

0

is this a local scirpt?

0

no, why would it be.

0

IF you using LocalPlayer, then the script has to be a local script or else it won't work

1 answer

You do not need to use :WaitForChild() on line 13. Also LocalPlayer wouldn't be a "Child" of player, more like an Instance of it. Player is the first thing made when a player enters the game. So therefore, that is not needed. You can simply define player as

game.Players.LocalPlayer

1 comment

0

Workspace.stats:32: attempt to index local 'player' (a nil value) which is what happens when i define player as game.players.localplayer. i know its reffering to the localplayer part, because when i take out the variable part, in the output it says Workspace.stats:31: attempt to index field 'LocalPlayer' (a nil value)

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