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

My kos shower gui is giving an error?

I get the error leaderstats is not a valid member of Player on line 4.

while wait(1) do
	local plyr = game.Players.LocalPlayer
	
	repeat wait() until plyr.leaderstats
	
	local kos = plyr.leaderstats.KOs
	
	script.Parent.Text = "KOs: " .. kos.Value
end

1 answer

✓ Accepted answer

When you use . to get a child, you get an error if that object does not exist -- you do not get nil.

If you aren't sure an object exists, you should use :FindFirstChild(name) instead.

If you just want to wait for an object to exist, you can use :WaitForChild. A caveat is that it will not stop waiting if the object is inserted before being given the proper name (i.e., you can't use leaderstats = Instance.new("IntValue", player) because it will have the name "IntValue" when added to the player)

It makes more sense to only get the player and kos once.

It's a good idea to use the actual spelling for a word. You don't get anything by shortening player by 2 letters, other than making it less readable and harder to type.

Your script only uses it two times, there's no reason to save 4 bytes!

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local kos = leaderstats:WaitForChild("KOs")

while wait(1) do
	script.Parent.Text = "KOs: " .. kos.Value
end

1 comment

0

Thanks!

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