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

Why is it saying that I'm staff instead of guest?

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)

1 comment

0

Is this a local script? If it is, change it. Also, are you checking from the explorer or the server?

2 answers

##Problem

Why is it saying that I'm staff instead of guest?

##Whats wrong 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

###Example

if "abc" then
	print"true"
else
	print"false"
end

>>true

##Solution 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.

###Example

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

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