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

Can someone tell what's wrong with this kill all script? Tried to loop it but it didn't work.

local victims = game.Players:GetPlayers()

script.Parent.MouseButton1Click:Connect(function()
	repeat 
		wait(1)
		print("Erro")
	until victims.Character
	victims.Character.Head:Destroy()
end)
	

It would be really helpful if someone showed me how to fix it. I'm still learning so I apologize for any mistakes.

2 answers

✓ Accepted answer

You're almost there.

victims is a table of all the players currently in the game. If you want to perform an action to every one of the elements of the table, you use a for loop.

local victims = game.Players:GetPlayers()
--[[
 this really looks like this
local victims = {Player1, Player2, Player3, ...}
]]--

script.Parent.MouseButton1Click:Connect(function()
	for i, victim in pairs(victims) do
	-- this is called a generic loop which loops through all the elements of a table
	-- `i` is the index, victim is the value (or the player), usually i is not used and is replaced with an underscore
	-- pairs(table) allows you to loop through the table giving you a key and a value to work with

		local humanoid = victim:FindFirstChild("Humanoid")
		if humanoid then
			humanoid.Health = 0 -- much less harsh than deleting their head
		end
	end
end)
	

Hope this helps! :)

3 comments

0

I thank you a lot! If we could have a chat, I'd really appreciate. It's really awesome to have nice people like you around,

0

Yeah no problem! Send over your discord and i'll add you.

0

Lucca#6865

Dont use repat.

while true do
	wait(1)
--etc
end

3 comments

0

what's wrong with "repat"?

0

Didn't get it.

0

You could even use "while wait(1) do end"

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