Event Detects Dead Player Problem

Hey,
So basically, I’m trying to make it award money when a person kills them, but when they reset, they can still kill them for money. Here is the code.

BountyDiedConnection = BountiedPlayer.Character.Humanoid.Died:connect(function()
		local creator = BountiedPlayer.Character.Humanoid:FindFirstChild("creator")
		if creator then
			print(creator.Value)
			local killer = creator.Value
			killer.leaderstats.Money.Value = killer.leaderstats.Money.Value + amount
			Unbounty(killer,false)
			BountyDiedConnection:Disconnect()
		end
	end)

However, when the character dies, it’s still trying to find its character from previously, which doesnt exist because he died.
I was wondering on how you could work around this?
Thanks.

Try making it redefine the creator variable every time to refresh it. A new character is made when the old one dies and the variable won’t auto-update, but will instead return to nil. You just need to redefine it when it is nil or when the function runs.

EDIT: I meant to redefine the BountiedPlayer variable instead of Creator

How would I go about redefining it? I’d need to redefine it within the event.

You could define the player in a value instead of their character, then whenever you need their character, you can do (variable).Character

EDIT: I meant to redefine the BountiedPlayer variable instead of Creator

I can try but I don’t think it’ll work as the the script might need to check the event again to see.

Is this the full script? (post to short so I had to add this)

No it’s not the full script but the rest doesn’t really matter.
Also I tested it and it didn’t work.
It doesn’t detect the change in variable.
It detects that the variable has changed but,
The event doesn’t change itself, if you die twice it only prints it once, as it’s still trying to see if the character died.

then you need to redefine what the character is

I have, here’s the script.

BountyDiedConnection = char.Humanoid.Died:connect(function()
		local creator = BountiedPlayer.Character.Humanoid:FindFirstChild("creator")
		if creator then
			print(creator.Value)
			local killer = creator.Value
			killer.leaderstats.Money.Value = killer.leaderstats.Money.Value + amount
			Unbounty(killer,false)
			BountyDiedConnection:Disconnect()
		else
			char = BountiedPlayer.CharacterAdded:wait()
			print("char added")
		end
	end)

I’m not too sure on what is going on, but you can try this. When they die, disconnect the event always. Then check for “creator” after disconnecting. Currently, you have the disconnect inside the if-end area which checks for “creator”.

Then, use Player.CharacterAdded to reinstate the connection.

I found a solution, basically I kind of did a recursive event, it disconnects on death and reconnects afterwards. Here’s how it works for any people who have the same type of problem in the future.

local function plrDied()
		local creator = BountiedPlayer.Character.Humanoid:FindFirstChild("creator")
		if creator then
			print(creator.Value)
			local killer = creator.Value
			killer.leaderstats.Money.Value = killer.leaderstats.Money.Value + amount
			Unbounty(killer,false)
			BountyDiedConnection:Disconnect()
		else
			BountyDiedConnection:Disconnect() -- Disconnect it so the event we fired isn't in use.
			BountyDiedConnection = BountiedPlayer.CharacterAdded:wait().Humanoid.Died:connect(plrDied) - Reconnect it, now its recursive.
			print("char added")
		end
	end
	
	
	BountyDiedConnection = BountiedPlayer.Character.Humanoid.Died:connect(plrDied) -- Fire event when died

Thanks but it’s not what I was looking for.

Sorry, the translation was not clear

Happy opportunity, nice to chat with you