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

How can I fix a scripts that kicks everyone except for a few players?

Asked by
H3kken -4
5 years ago
Edited by User#24403 5 years ago

I want to make a script that kicks everyone in the server except for 3 players: "H3kken", "17alwin17" and "Is3nty".

I tried making it this way:

for i,v in pairs(game.Players:GetChildren(plr)) do
    if  plr.Name == "H3kken" or "17alwin17" or "Is3nty" then
        print("Left player behind in server")
    else
        v:Kick("This server has shut down.")
    end
end
0
Don't forget to accept my answer if it helps. User#24403 69 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Before you fix your problem you must first know truthy values and falsey values.


Truthy values

Truthy values in Lua are anything that is not false or nil. Strings, numbers and tables are examples of truthy values.

Falsey values

Falsey values are just false or nil.

Logical operators

or, and, not are all logical operators.

  • x or y evaluates to x if x is truthy, y otherwise.

  • x and y evaluates to x if x is falsey, y otherwise

  • not x evaluates to true if x is falsey, false otherwise.

Fixing the problem

Now that you know truthy values and how logical operators work, let's fix the codes.

:GetChildren() doesn't take any arguments and you never defined plr anywhere.

It's possible to place non-Player instances in the Players service (you shouldn't anyways but it's still possible) so you should use :GetPlayers().

You can use a dictionary with the names and check if their name is in the dictionary like so

local playersToNotKick = {
    ["H3kken"] = true,
    ["17alwin17"] = true,
    ["ls3nty"] = true
}

for _, player in pairs(game.Players:GetPlayers())) do -- player is much better than v or plr! 
    if playersToNotKick[player.Name] then
        print("Left player behind in server")
    else
        player:Kick("This server has shut down.")
    end
end
0
Thank you very much! H3kken -4 — 5y
Ad

Answer this question