Detect Kill and Reward is not working

Hello,
I’m trying to make a kill and reward system. Whenever a player kills another player, the player who survived should earn coins.
I got the leaderboard working and also the save system. The problem is rewarding the player.

What I have is a sword, and inside the sword there’s a script that detects whenever the sword collides with an other object. If the other object is a player, then it will write the player’s name on a variable inside the target player’s starter gui. This variable inside the target player can change if someone else hits him and it serves as a “last attacker” variable, as in, it stores the name of the player’s last attacker.

All this works fine!

Then what I want to do is whenever the target player dies, it checks the last attacker’s name and awards them 3-10 coins.
Here is where the problem arrives. Whenever I try to award the last attacker of the player, the console tells me the value is nil, even though I can see in the explorer that the variable has the name of a player.

NOTE: I am not using local scripts, it is all server-side scripts, so it has nothing to do with remote functions.

This script below goes inside the sword, it is responsible for overriding the last attacker’s value inside the player. (Is working)

local handle = script.Parent.Handle
local localplayer = script.Parent.Parent.Parent
local humanoid = localplayer.Character.Humanoid

handle.Touched:Connect(function(part)
	if part.Parent:FindFirstChild("Humanoid")then
		local tcharacter = part.Parent
		local tplayer = game.Players:FindFirstChild(tcharacter.name)
		print(tplayer.Name)
		tplayer.PlayerGui.Attacker.Attacker.Value = localplayer.Character.Name
	end
end)

This is the script inside the target player. Its job is to award the last attacker whenever it dies. (Isn’t working)

local Attacker = script.Attacker.Value
local Humanoid = script.Parent.Parent.Character.Humanoid

Humanoid.Died:Connect(function()
	local killer = game.Players:FindFirstChild(Attacker)
	killer.leaderstats.Coins.Value = killer.leaderstats.Coins.Value + 1
end)

As you can see here, Player2 was attacked by Player1

And when the target player dies, it says the value is nil.

Any help would be appreciated, and if there’s a better method to do this, please say so!

Thanks in Advance!

3 Likes

Are you changing the value of the Attacker variable in the script?
If not you can try something like this:

local Attacker = script.Attacker.Value
local Humanoid = script.Parent.Parent.Character.Humanoid

script.Attacker.Changed:Connect(function(new_value)
    Attacker = new_value
end)

Humanoid.Died:Connect(function()
	local killer = game.Players:FindFirstChild(Attacker)
	killer.leaderstats.Coins.Value = killer.leaderstats.Coins.Value + 1
end)
2 Likes

Thank you very much! that worked!