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

How do I check if a player is in the table?

Asked by
Prioxis 673 Moderation Voter
9 years ago

So i'm making a frame that hides the game from people unless their on the list (I know I could just do friends only but I have a lot of friends)

and so if their on the list then the gui gets hidden else make it shown

heres the script

print'Player Kicker'
admins={"ProjectRosploit","AlexTheOtaku","Lisforlucas","Ray5069"} -- admin table
game.Players.PlayerAdded:connect(function(playa)
function tableContains(t, value) -- function to check for player
    for _, v in pairs(t) do
        if v == value then 
            return true
        end
    end
    return false
if playa and TableContains(admins, player.Name) then -- statement gets an error
    -- don't know what to put here
end
end
end)
0
If value is a player do value.Name EzraNehemiah_TF2 3552 — 9y

3 answers

Log in to vote
5
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

Your Problem

The parameter for your PlayerAdded event was playa, then you called the TableContains function using player.

player isn't defined! You have to call the TableContains function with playa.

But, you also called the TableContains function inside of itself, before closing the function with an end statement.


Fixed Code

local admins = {"ProjectRosploit","AlexTheOtaku","Lisforlucas","Ray5069"}

function tableContains(t, value)
    for _, v in pairs(t) do
        if v == value then 
            return true
        end
    end
    return false
end

game.Players.PlayerAdded:connect(function(playa)
    if playa and TableContains(admins, playa.Name) then

        --Your Code

    end
end)

Efficiency Edits

Make the table, a dictionary with players' names! That way you can index the table like admins[playa.Name] to see if the player is there, and you can eradicate the whole tableContains function completely.


Goulified Code

local admins = {
    ["ProjectRosploit"] = true,
    ["AlexTheOtaku"] = true,
    ["Lisforlucas"] = true,
    ["Ray5069"] = true
}

game.Players.PlayerAdded:connect(function(playa)
    if admins[playa.Name] then

        --Your Code

    end
end)
1
"Goulified" Code haha I like it! :) AmiracIe 175 — 9y
0
Thx lol Goulstem 8144 — 9y
Ad
Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

Your statement returns an error because player in player.Name is not defined. You probably meant playa.Name.

Also, format your code in an organized manner!

print'Player Kicker'
admins = {"ProjectRosploit","AlexTheOtaku","Lisforlucas","Ray5069"} 

game.Players.PlayerAdded:connect(function(playa)
    function tableContains(t, value)
        for _, v in pairs(t) do
            if v == value then 
                return true
            end
        end
        return false
        if playa and TableContains(admins, playa.Name) then

        end
    end
end)

The issue seems to be located in the function itself.

  • The function is not called outside of its scope.

  • The "if" is in the wrong place.

Let's make the function a static one.

print'Player Kicker'
admins = {"ProjectRosploit","AlexTheOtaku","Lisforlucas","Ray5069"}

function tableContains(t, value)
    for _, v in pairs(t) do
        if v == value then 
            return true
        end
    end
    return false
end

game.Players.PlayerAdded:connect(function(playa)
    if playa and TableContains(admins, playa.Name) then

    end
end)

Now we're getting somewhere!

Now, for your GUI, I assume it's already in game.StarterGui. So, let's define it.

print'Player Kicker'
admins = {"ProjectRosploit","AlexTheOtaku","Lisforlucas","Ray5069"}

function tableContains(t, value)
    for _, v in pairs(t) do
        if v == value then 
            return true
        end
    end
    return false
end

game.Players.PlayerAdded:connect(function(playa)
    if playa then
        local PlayerGui = playa:WaitForChild("PlayerGui")
        local Gui = PlayerGui:WaitForChild("ScreenGui") -- Or however you want to name it.
        local Frame = Gui:WaitForChild("Frame")
        if TableContains(admins, playa.Name) then
            Frame.Visible = false
        else
            Frame.Visible = true
        end
    end
end)

Since game.StarterGui replicates its children to the Player's PlayerGui, the GUIs will be visible to the people in the list. So let's wrap the "if" statement with a .CharacterAdded event.

print'Player Kicker'
admins = {"ProjectRosploit","AlexTheOtaku","Lisforlucas","Ray5069"}

function tableContains(t, value)
    for _, v in pairs(t) do
        if v == value then 
            return true
        end
    end
    return false
end

game.Players.PlayerAdded:connect(function(playa)
    playa.CharacterAdded:connect(function(Character)
    -- In this case, you don't need to see if the "playa" exists.
        local PlayerGui = playa:WaitForChild("PlayerGui")
        local Gui = PlayerGui:WaitForChild("ScreenGui") -- Or however you want to name it.
        local Frame = Gui:WaitForChild("Frame")
        if TableContains(admins, playa.Name) then
            Frame.Visible = false
        else
            Frame.Visible = true
        end
    end)
end)
Log in to vote
-3
Answered by 9 years ago

If you want it so that if you want to "Ban" or kick people out of your game so people don't join, use a table, and use the Kick method.


Kick

It kicks the player, if you add a string in between the "()"s then you can add a custom message

game:GetService("Players").Player1:Kick("You have been kicked from the server!")


Final Product

local message = "You are not allowed in this game." --Add a message here
allowed = {"ProjectRosploit","AlexTheOtaku","Lisforlucas","Ray5069"}

game:GetService("Players").PlayerAdded:connect(function(plyr)
    local IsAllowed = false
    for _,allow in pairs(allowed) do
        if plyr.Name == allow then
            IsAllowed = true
        end
    end
    if not IsAllowed then
        plyr:Kick(message)
    end
end)



Hope it helps!

Answer this question