Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does this problem with my death handler occur?

Hey, so, I made an event that when the player dies, they lose half of their money(Platinum). However, the problem is that it only does that once, so it only counts when I die for the first time. I'll post my scripts now, please, tell me what's wrong here.

RemoteEvent:

local remotevent = script.RemoteEvent

remotefunction.OnServerEvent:Connect(function(player)
	player.leaderstats.Platinum.Value = player.leaderstats.Platinum.Value / 2
end)

Death handler:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

hum.Died:Connect(function()
	game.Workspace.deathslol.RemoteEvent:FireServer()
end)

4 answers

✓ Accepted answer

The problem here is that everytime your character dies, the model gets removed and replaced. Because of this, the humanoid variable you set earlier becomes nil, making the event invalid. To fix this, I would recommend putting the localscript in PlayerScripts>StarterPlayerCharacter, and it should fix it.

local char = script.Parent --Any localscript put in StarterPlayerCharacter becomes a child of the character when it is loaded in.
local hum = char:WaitForChild("Humanoid")

hum.Died:Connect(function()
	game.Workspace.deathslol.RemoteEvent:FireServer()
end)

This is the exact same script of yours, with a bit less code because of the new script location.

Another way to fix it is to update the character AND humanoid variables every time the character respawns, but thats a bit more complicated.

I assume you're trying to make it so that it only halves when you die the FIRST time, and the answer is simple: debounce. Change your death handler into this:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local debounce = false

hum.Died:Connect(function()
	if debounce == false then
		debounce = true
		game.Workspace.deathslol.RemoteEvent:FireServer()
	end
end)

4 comments

0

No, that is not it. That is actually, the problem. It only halves it the first time you die, but it's meant to actually halve it every time you die.

0

no .Died only happens once, it doesnt need debounce to not run multiple times

0

Gameplayer, no need to downvote my script just because there's a unnecessary debounce.

0

I'm making sure, also nice job downvoting EVERY answer.

I'm not sure this will work. I'm thinking the char variable is the problem.

local plr = game.Players.LocalPlayer
local char = repeat wait() until plr:GetCharacterFromPlayer(char)
local hum = char:WaitForChild("Humanoid")

hum.Died:Connect(function()
    game.Workspace.deathslol.RemoteEvent:FireServer()
end)

or

local plr = game.Players.LocalPlayer
local char = repeat wait() until plr.Character
local hum = char:WaitForChild("Humanoid")

hum.Died:Connect(function()
    game.Workspace.deathslol.RemoteEvent:FireServer()
end)

or

local plr = game.Players.LocalPlayer
local char = repeat wait() until plr:FindFirstChild("Head")
local hum = char:WaitForChild("Humanoid")

hum.Died:Connect(function()
    game.Workspace.deathslol.RemoteEvent:FireServer()
end)
game.Players.LocalPlayer.CharacterAdded:Connect(function(char)
	char:WaitForChild("Humanoid").Died:Connect(function()
		 game.Workspace.deathslol.RemoteEvent:FireServer()
	end)
end)

pretty simple, when your character has been added we give it the character as an argument, when the humanoid inside that character dies, we connect the function that says "fire"

This discussion is preserved as it was. New answers, comments, and votes are closed.