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

Checking if a table contains a string?

Asked by
OKRPLAY 25
6 years ago

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
1
`if type(v) == "string" then` creeperhunter76 554 — 6y
0
ipairs takes longer than pairs. hiimgoodpack 2009 — 6y

1 answer

Log in to vote
0
Answered by
H4X0MSYT 536 Moderation Voter
6 years ago
Edited 6 years ago

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.

0
Thank you! OKRPLAY 25 — 6y
0
I needed to remove the return in if Plr.Name == v. OKRPLAY 25 — 6y
Ad

Answer this question