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

Why does this problem with my death handler occur?

Asked by 4 years ago

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

Log in to vote
0
Answered by
Kyokamii 133
4 years ago
Edited 4 years ago

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.

Ad
Log in to vote
-1
Answered by 4 years ago

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)

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. AddisonAvery 43 — 4y
0
no .Died only happens once, it doesnt need debounce to not run multiple times Gameplayer365247v2 1055 — 4y
0
Gameplayer, no need to downvote my script just because there's a unnecessary debounce. AcrylixDev 119 — 4y
0
I'm making sure, also nice job downvoting EVERY answer. AcrylixDev 119 — 4y
Log in to vote
-1
Answered by
Geobloxia 251 Moderation Voter
4 years ago

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)
Log in to vote
-1
Answered by 4 years ago
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"

Answer this question