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

Died event isn't (or doesn't appear to) fired when a player dies?

Asked by 9 years ago

Game Part Summary: So in my game, when someone dies all the contents of their backpack are thrown into the workspace as ammo pickups, including the amount of ammo they have.

So here is the script that does that:

local player = game.Players.LocalPlayer
while player.Character == nil do wait(1) print "Waiting for character... " end --Waits for character
local char = player.Character
print "Character Found!"
char:WaitForChild("Humanoid") --Waits for humanoid
char.Humanoid.Died:connect(function() --Waits for humanoid to die
    print "Someone died"
    char.Humanoid:UnequipTools()--Puts tools in backpack

    local backpack = player.Backpack:GetChildren()
    for i = 1, #backpack do --Loops through player's backpack and puts it into the workspace as an ammo pickup
        if backpack[i] ~= script and backpack[i]:IsA("Tool") == true then 
            print("Dropping "..backpack[i].Name.." tool")
            backpack[i].Handle.Position = char.Torso.Position
            backpack[i].Handle.Name = "AmmoPickup"
            backpack[i].AmmoPickup.Script.Disabled = false
            backpack[i].AmmoPickup.Parent = workspace
        end
    end
    print ("Spitting out "..script.Parent.Ammo.Value.." rockets")
    for i = 1, script.Parent.Ammo.Value do --Spits out the amount of rockets the player had
        local clone = game.ReplicatedStorage.Rocket:Clone()
        clone.Position = char.Torso.Position
        clone.Parent = workspace
    end
end)

Script Location: This script starts in the StarterPack, and as expected, is cloned to other player's Backpacks. This is a LocalScript

Problem: This script works when players are on a "Spectator" team (i'll change that later; the spectating area fills up with rockets otherwise <_>) but doesn't work when the player is killed so they can be moved to another team. Once they are on that team, the Diedevent never fires when they are killed by resetting or being hit by a rocket (the print functions show me so.) If they move back to Spectators it no longer works. Basically, the event is only ever fired once.

So what am I doing wrong and how can I fix it?

1 answer

Log in to vote
3
Answered by
Unclear 1776 Moderation Voter
9 years ago

Died doesn't fire reliably client-side on your own humanoid if your LocalScript operates in a location that is reset every respawn. You should only use it server-side.

Please use HealthChanged instead...

You can simulate basically the same behavior as Died with the following code

-- assuming the variable `humanoid` points to the Humanoid of the character
humanoid.HealthChanged:connect(function(health)
    if health == 0 then
        -- died
    end
end)
Ad

Answer this question