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

Does anyone know why kill all does not work on a touched event?

Here is the script:

game.Workspace.Part.Touched:Connect(function(hit)
	for i, player in ipairs(game.Players:GetChildren()) do
    	player.Character.Humanoid.Health = 0
	end
end)

When the game starts up it just kills you, but after that it's fine. Does anyone have a solution?

1 comment

0

I think ipairs is supposed to be pairs, and it is recommended to use GetPlayer()s instead of GetChildren() when you're getting the players from the Players Service.

2 answers

✓ Accepted answer

i'm a little confused by the question. you say it works fine, however, in the title, it says it doesn't work...?

well, either way, the reason it kills you right away is because the touched event fires the moment the part touches something. you don't have it detect the humanoid of the player(s) who touched it.

here's an example of how you'd go about this:

game.Workspace.Part.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h then
		for i, player in ipairs(game.Players:GetChildren()) do
			player.Character.Humanoid.Health = 0
		end
	end
end)

1 comment

0

lol im so dumb i didnt anchor the part

Its becaused the script target a single player, you need to change it so it will target every player in the local function

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