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