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

My kos shower gui is giving an error?

Asked by
Relatch 550 Moderation Voter
8 years ago

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

Log in to vote
5
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

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
0
Thanks! Relatch 550 — 8y
Ad

Answer this question