Ad
Log in to vote
0

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

Asked by
YT_Epiix -10
4 months 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 135 — 4mo

2 answers

Log in to vote
0
Answered by
Shematics 141
4 months ago

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

Ad
Log in to vote
0
Answered by 4 months 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 YT_Epiix -10 — 4mo

Answer this question