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)
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)
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"