I am trying to make a loop kill command

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).

1 Like

Are you trying to get the char from the player’s folder? I think you need to get the player’s character doing a different loop.

Example:

for i, v in pairs(game.Players:GetPlayers()) do
local character = v.Character
if character then
-- do stuff
end
end

That is a solution but I need it to loop through constantly which is why I used a while loop.

Then, put a while wait() do above the for i, v loop.

while wait() do
for i, v in pairs(game.Players:GetPlayers()) do
local character = v.Character
if character then
-- do stuff
end
end
end

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.

1 Like

Not sure if this is the solution but try this

while true do
     for _,player in ipairs(game.Players:GetPlayers()) do
          local character = player.Character
          if character then
               character:BreakJoints()
          end
     end
end
1 Like
-- 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! :smiley:

3 Likes

It still comes up with attempt to index nil with ‘Character’ :thinking:

Never mind, it works now! I modified the script a bit.

2 Likes