I am trying to make a loop kill admin command and so I am planning on setting up a table of all the players to loop kill and use table.insert to insert a new player and table.remove to remove the player from the table when they leave, all of that works except for the actual loop itself.
while wait() do
for i, player in pairs(loopkillPlayers) do
local char = game.Workspace:FindFirstChild(player)
if char:FindFirstChild("Humanoid").Health > 0 then
char:FindFirstChild("Humanoid").Health = 0
end
end
end
(I put my username into the table to test and it doesn’t work).
Yeah I am using a table to contain all the loop kill players which is why I am looping through the items in the table instead of looping through players, but it can’t find the humanoid of the character.
while true do
for _,player in ipairs(game.Players:GetPlayers()) do
local character = player.Character
if character then
character:BreakJoints()
end
end
end
-- Loop kill players
local loopKillConnections = {}
for i, player in pairs(loopkillPlayers) do
player = game:GetService("Players"):FindFirstChild(player) -- Find player instance
local char = player.Character or player.CharacterAdded:Wait() -- if the player's character doesn't already exist we wait for it to exist
char:BreakJoints() -- Kill
local onCharAdded = player.CharacterAdded:Connect(function(char)
char:BreakJoints() -- Everytime the character is added again we kill them again
end)
table.insert(loopKillConnections, {Name = player.Name, Connection = onCharAdded}) -- Add to a table with all the other event, this will allow us to stop loop killing players
end
Because of that loopKillConnections table, we can also stop loop killing people like so:
-- Stop loop killing
for i, player in pairs(unloopKillPlayers) do
for ii, vv in pairs(loopKillConnections) do
if vv.Name == player.Name then
vv.Connection:Disconnect() -- Stop the event from firing
loopKillConnections[ii] = nil -- Remove the connection from the table
break -- Stop looping
end
end -- Find the targets onCharAdded event
end
Hopefully this helped, please tell me if there are any errors!