Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

1 comment

0

Don't forget to accept my answer if it helps.

1 answer

✓ Accepted answer

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

1 comment

0

Thank you very much!

This discussion is preserved as it was. New answers, comments, and votes are closed.