So basically when a player joins it's supposed to check if their userid is in the table, if it is then their rank value will be staff, if not, it will be guest. I didn't put my ID in the table but it still sets my value as staff instead of guest.
local staff = {"11", "131226890", "169602215"} game.Players.PlayerAdded:Connect(function(plr) local userid = plr.UserId local leader = Instance.new("Folder") leader.Name = "leaderstats" leader.Parent = plr local rank = Instance.new("StringValue") rank.Name = "RANK" rank.Parent = leader for i, userid in pairs(staff) do if userid then rank.Value = "STAFF" else rank.Value = "GUEST" end end end)
Why is it saying that I'm staff instead of guest?
First you're not even comparing if the player's UserId
and the userid
when iterating are equal. Second you're overwriting your original reference to userid
. In this case you're checking if userid
has truthiness. Strings have truthiness and so the conditional will always be true
if "abc" then print"true" else print"false" end
>>true
Use a different variable name and a actually compare whether they are equal when iterating. Also we will want to use tostring
on plr.UserId
since the indexes inside our staff
array are strings
Also we are using break
to stop the loop when they're staff to prevent it from continuing.
local staff = {"39939779", "131226890", "169602215"} game.Players.PlayerAdded:Connect(function(plr) local userid = tostring(plr.UserId) local leader = Instance.new("Folder") leader.Name = "leaderstats" leader.Parent = plr local rank = Instance.new("StringValue") rank.Name = "RANK" rank.Parent = leader for i, staffUserId in pairs(staff) do if staffUserId == userid then rank.Value = "STAFF" break else rank.Value = "GUEST" end end end)
To fix this do this:
local staff = {"11", "131226890", "169602215"} game.Players.PlayerAdded:Connect(function() local userid = plr.UserId local leader = Instance.new("Folder") leader.Name = "leaderstats" leader.Parent = plr local rank = Instance.new("StringValue") rank.Name = "RANK" rank.Parent = leader rank.Value = "Guest" for i,v in pairs(staff) do if userid == v then rank.Value = "Staff" end end end)
Simple