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

Comparing strings not working with if statement?

Asked by 2 years ago
Edited 2 years ago

I'm trying to set up a system to check to see if the user is an admin and if they are they are given the admin panel that I have made.

Here's the code Ps its in the ServerScriptService if that matters at all

-- Give Admin Panel function
local Admins = {"Bosskiller177", "Test 2"}
local function checkPlayerForAdmin(plr)

    local adminPanel = game:GetService("StarterGui")["Admin Panel"] -- Gets the Admin Panel
    for index = 1, #Admins do

        if Admins[index] == plr then
            print("Admin")
            adminPanel.Enabled = true
            break
        else
            print("Not Admin")
            adminPanel.Enabled = false
        end

    end
end


game.Players.PlayerAdded:Connect(function(plr)
    checkPlayerForAdmin(plr)
end)

I've looked all over but the results are either to complex for me or just doesn't work Why is lua being like this, any other language ive learnt makes this process so easy, but not with lua

1 answer

Log in to vote
0
Answered by
Pupppy44 671 Moderation Voter
2 years ago

The problem with this code is that you're trying to compare a Player object with a string.

if Admins[index] == plr then

plr isn't a string or the name of the player that joined, but rather the Player object itself. To fix this problem, you can compare the string with the Name property of the player, aka. the username.

if Admins[index] == plr.Name then
Ad

Answer this question