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

Checking if a table contains a string?

Im currently scripting an admin gui and i ran into a problem: im using ipairs to check if the player is in the admin table. Now Player1 is ~= my name so it deletes the gui. Is there a function to check if a table contains a string?

plr = game.Players.LocalPlayer

admins = {"OKRPLAY","Player1"}

for i,v in ipairs(admins) do
	if v ~= plr.Name then
		script.Parent:Destroy()
	end
end

2 comments

1

if type(v) == "string" then

0

ipairs takes longer than pairs.

1 answer

✓ Accepted answer

There are some issues with your current script. Let's address these first. 'if v ~= plr.Name then' we'll say my name is 'Player1'. I'm an admin. But since it goes through all of the strings, if my name isn't 'OKRPLAY' then it will be deleted. With slight changes we end with this. If you don't want a function, slight changes will fix that.

local Admins = {"TeleMom", "Noobite"}
function CheckAdmin(Plr)
	local Success = false
	for i,v in pairs(Admins) do -- Go through the admin list
		if Plr.Name == v then
			Success = true  -- Indicate that it was a success
			return                 -- Player is an admin, don't waste time going through.
		end
	end
	return Success
end

If the player is an admin then you get true. If not, you get a false.

2 comments

0

Thank you!

0

I needed to remove the return in if Plr.Name == v.

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