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 10 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:

01local player = game.Players.LocalPlayer
02while player.Character == nil do wait(1) print "Waiting for character... " end --Waits for character
03local char = player.Character
04print "Character Found!"
05char:WaitForChild("Humanoid") --Waits for humanoid
06char.Humanoid.Died:connect(function() --Waits for humanoid to die
07    print "Someone died"
08    char.Humanoid:UnequipTools()--Puts tools in backpack
09 
10    local backpack = player.Backpack:GetChildren()
11    for i = 1, #backpack do --Loops through player's backpack and puts it into the workspace as an ammo pickup
12        if backpack[i] ~= script and backpack[i]:IsA("Tool") == true then
13            print("Dropping "..backpack[i].Name.." tool")
14            backpack[i].Handle.Position = char.Torso.Position
15            backpack[i].Handle.Name = "AmmoPickup"
View all 26 lines...

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
10 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

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

Answer this question