Hello. I am adding commands to a command system I am working on for my game.
One command in specific is !kickOGS, and it is made for streamers. Let’s say for example the argument given is 4. The command will go through every player and kick the four oldest players in the server. (Oldest being the first to join the server). How would I do this?
By the way, all players get an intvalue assigned to them showing the os.time() they joined at.
You would iterate through the list you created for the players and check the current time and compare that with their join time.
However, I would suggest using numerical indices for each player:
-- Example
local playerList = { -- 5 players who joined (in order)
[1] = Player1,
[2] = Player2,
[3] = Player3,
[4] = Player4,
[5] = Player5
}
-- Kick the oldest players
for _ = 1, 4 do
local player = playerList[1] -- When the previous player is removed, the next oldest player automatically moves into the first slot
player:Kick()
table.remove(playerList, 1) -- Remove the player from the list so it can shift downward and auto-resize the list
end
local kicklist = {}
local exception = {309545016,game.PrivateServerOwnerId,p.UserId}
repeat
local oldest = {nil,9999999999999999}
for i, v in pairs(game.Players:GetPlayers()) do
local found = false
for _,n in pairs(kicklist) do
if n[1] == v then
found = true
end
end
if v.Stats.JoinTime.Value < oldest[2] and found == false then
oldest[2] = v.Stats.JointTime.Value
oldest[1] = v
end
end
if oldest[1] then
table.insert(kicklist,(#kicklist+1),oldest[1])
end
until #kicklist >= arguments[1]
for i, v in pairs(kicklist) do
v:Kick("Kicked my server admin. Please join a new server.")
end
local kicklist = {} -- table of players to kick
local ignored = {309545016,game.PrivateServerOwnerId,p.UserId} -- IDs to ignore
for i, v in (Players:GetPlayers()) do
if (table.find(ignored, v.UserId)) then continue end -- Ignore this player
if (#KickList < Amount) then -- We need players we can compare
local TimePlayed = os.time() - PlayerIntValue.Value -- The time that has passed since the player has joined the game
table.insert(kicklist, {v, TimePlayed}) -- {Player, TimePlayed}
else
--<< Compare the time of this player with all players in the KickList table >>--
local TimePlayed = os.time() - PlayerIntValue.Value
for i, tbl in (kicklist) do
if (TimePlayed > tbl[2]) then -- We check if the time played value is greater than the current index's time played
kicklist[i] = {v, TimePlayed} -- We update this index with the new player and their time played
break -- Break the loop
end
end
end
end
for _, v in pairs(kicklist) do
--<< Kick the players in the table >>--
if (v[1] ~= nil) then -- v[1] is the player object
v[1]:Kick()
end
end
local PlayersJoined = {}
game.Players.PlayerAdded:Connect(function(Player)
table.insert(PlayersJoined, {Player, time()})
end)
game.Players.PlayerRemoving:Connect(function(Player)
for i, Info in ipairs(PlayersJoined) do
if Info[1] == Player then
table.remove(PlayersJoined, i)
end
end
end)
-- ... code
Function = function(p,arguments)
local kicklist = {}
local kickAmount = table.unpack(arguments)
local exception = {309545016, game.PrivateServerOwnerId}
for i=1, kickAmount do
local lowestPlayer, LowestTime = nil, nil
for i, Info in pairs(PlayersJoined) do
local Player, Time = table.unpack(Info)
if table.find(exception, Player.UserId) then
continue
end
if not Time or Time < LowestTime then
lowestPlayer = Player
LowestTime = Time
end
end
if not lowestPlayer then
break
end
table.insert(kicklist, lowestPlayer)
end
for i, Player in pairs(kicklist) do
Player:Kick()
end
end
-- Get list of players at the current moment
local playerList = game.Players:GetPlayers()
-- Remove any exceptions so they are not kicked
for i, player in ipairs(playerList) do
for _, id in ipairs(exceptions) do
if player.UserId = id then
table.remove(playerList, i)
end
end
end
-- Sort the remaining players based on their join time
table.sort(playerList, function(a, b)
return a.Stats.JoinTime.Value < b.Stats.JoinTime.Value
end)
-- Kick the oldest players
for i = 1, math.min(numberToKick, #playerList) do
playerList[i]:Kick()
end
Do you think you could insert a value into the player when they join with a seperate script, make it count up every second and then find the players with the highest values inside of them and kick them.