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

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

Asked by 5 years ago

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)
0
Is this a local script? If it is, change it. Also, are you checking from the explorer or the server? MahadTheIronSword 98 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

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)
Ad
Log in to vote
0
Answered by 5 years ago

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

Answer this question