So this is probably a very basic question, but I’ve searched on the DevForum and else where to find nothing. My issue is very basic, kick players randomly from a game. Now I’m pretty sure this has to do with getting the number of players or a for/while loop but getting said players is what I’m struggling with.
local players = game:GetService("Players")
local player_count = players:GetChildren()
if #player_count >= 2 (or the number of players wanted) then
--kick players randomly here and getting said players from the players service
end
Also note this isn’t my exact code, it’s the base of what I want in a sense. Any help is appreciated, thanks in advanced,
~Notrealac0unt
Here is a simple function I put together to kick a given amount of random players.
local Players = game:GetService("Players")
local function kickRandomPlayers(amount)
for _ = 1, amount do
Players:GetPlayers()[math.random(#Players:GetPlayers())]:Kick()
end
end
kickRandomPlayers(5) -- will kick 5 random players
If you wanted to kick enough players until there was two left (which is what I am guessing you are trying to do) you could use the function like so:
this script will kick some players depending on what number they get
local players = game:GetService("Players")
local player_count = players:GetChildren()
for i, v in pairs(player_count) do
local kick = math.random(1,#player_count)
if kick == 1 then
kick that player
end
end
i didn’t include your whole script but you get the idea
You’re on the right track with your player_count variable.
local players = game:GetService("Players")
while true do --// Infinite loop
local player_count = players:GetChildren() --// Get the current number of players each iteration
if (#player_count >= 2) then --// Checks if there are enough players to start kicking
local rnd = math.random(1, #player_count) --// Random # between 1 and the max players
player_count[rnd]:Kick("Randomly kicked") --// Kicks the player at that number
print("Kicked " .. player_count[rnd].Name)
end
wait(5)
end