Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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?

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. oilsauce 196 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

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)
0
lol im so dumb i didnt anchor the part ElusiveEpix 2 — 5y
Ad
Log in to vote
0
Answered by
Shematics 117
5 years ago

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

Answer this question