Kill for cash using only gui is not working

So I tried to create a system that if you kill a player you get cash and you can see how much cash you got by only gui and no leaderboard, but it didn’t work. Here is the script that I tried to do.

game.Players.PlayerAdded:Connect(function(plr)
local Stats = Instance.new(“Folder”)
Stats.Name = “stats”
Stats.Parent = plr
local Money = Instance.new(“IntValue”)
Money.Name = “Money”
Money.Value = 0
Money.Parent = Stats
plr:WaitForChild(“Humanoid”).Died:connect(function(killed)
local CreatorTag = plr.Humanoid:FindFirstChild(“creator”)
if CreatorTag and CreatorTag.Value then

		local stats = CreatorTag.Value:WaitForChild("leaderstats")

		stats.Money.Value = stats.Money.Value + 20
	end
end)

end)

This script is in the ServerScriptService.
Capture
This gui shows how much cash I have.
Thx for helping me out if you have an answer to this!
(Btw this is my first post)

1 Like

So is the person who should get the money, get it, and the ui isn’t updating, or is the problem that the player who got the kill didn’t get the money?

Humanoid is not a child of the player instance but the player character’s. Also the .Died event doesn’t pass any argument to the callback function so that killed parameter will always be nil.

Change plr.Humanoid with plr.Character.Humanoid.

I changed plr.Humanoid with plr.Character.Humanoid and deleted the .Died event (I might have done It wrong) and it still didnt work.

game.Players.PlayerAdded:Connect(function(plr)
local Stats = Instance.new(“Folder”)
Stats.Name = “stats”
Stats.Parent = plr
local Money = Instance.new(“IntValue”)
Money.Name = “Money”
Money.Value = 0
Money.Parent = Stats
plr:WaitForChild(“Humanoid”):connect(function(killed)
local CreatorTag = plr.Character.Humanoid:FindFirstChild(“creator”)
if CreatorTag and CreatorTag.Value then

		local stats = CreatorTag.Value:WaitForChild("leaderstats")

		stats.Money.Value = stats.Money.Value + 20
	end
end)

end)

the player who got the kill didnt get the money.

Name it “leaderstats” instead of “stats”?

that’s only going to change into the leaderboard, and I don’t want that. Or am I wrong?

Oh wait I kind of zoomed through your post i didnt realize you were trying to use a custom leaderboard, my bad.

It’s okay. At least you tried to help me, I appreciate it!

2 Likes

What you should do is maybe go into the script that performs the action of killing/damaging the another player, and then update the leaderboard from there if that player dies

With my previous post I obviously also meant to change the WaitForChild() function to plr.Character:WaitForChild("Humanoid"). Anyway you didn’t tell us what the creator tag value is parented to.

Finally, please format your code here on the forum with code blocks: put three backticks (```) at the top and at the bottom of your code. It helps us have an easier read of your code.

1 Like

Capture
Hmmm, how do I do this part?
(I’m so sorry I tried to figure this out on my own and I found out that I put there for no reason)

What do you put the creator tag value into? Is it the humanoid?

local CreatorTag = plr.Character.Humanoid:FindFirstChild("creator")
if CreatorTag and CreatorTag.Value then
Yea I have I believe, and I tried to build a code block to make things simpler.

You don’t seem to understand what you are doing too well, it looks like it’s a free model so let me give you a general idea of how to achieve what you want. You will need to create a value somewhere in the player’s character or instance and then assign the user ID of the killer to it, then gather that value when the player dies to give them the money.

Let’s suppose that your weapon is a sword, you will need to detect when the sword hits a player and set the killer value to the sword’s owner user ID, add a Humanoid.Died event listener so that you can know when the player dies and then get the value of the user who killed the player to give them the money.

The sword is what I am exactly using and I just need to understand how to set the killer value to the sword’s owner user id. I also would probably need to change this line.
plr.Character:WaitForChild("Humanoid"):connect(function(killed)
Oh and I forgot to put this local script in to help you out more. This local script is used on a textlabel as a gui.
local plr = game.Players.LocalPlayer
local Stats = plr:FindFirstChild("stats")

script.Parent.Text = Stats.Money.Value

Stats.Money.Changed:Connect(function(NewValue)
script.Parent.Text = NewValue
end)
I am pretty sure this script would have problems too. I think im getting closer to fixing the problem.

Alr so let me give you a template for this.

Put the following into the sword tool:
Script

Script in the sword tool

local hitbox = -- part that hits players

hitbox.Touched:Connect(function(hitPart)
    local hitChar = hitPart:FindFirstAncestorOfClass("Model") -- get the character model that was hit
    local char = hitbox:FindFirstAncestorOfClass("Model") -- get the character of the player holding the sword, you can use this because the tool is moved into the player character when it is equipped
    
    if not hitChar:FindFirstChild("Humanoid") then
        return
    end

    local hitPlayer = game.Players:GetPlayerFromCharacter(hitChar) -- get the hitted player instance
    local player = game.Players:GetPlayerFromCharacter(char)

    hitPlayer.Killer.Value = player -- even if the player doesn't die set the killer so that you can know who killed the player once they die
end)

Script in ServerScriptService

game.Players.PlayerAdded:Connect(function(player)    
    player.CharacterAdded:Connect(function(char)
        local humanoid = char:WaitForChild("Humanoid")

        local killer = Instance.new("ObjectValue")
        killer.Name = "Killer"
        killer.Parent = player

        humanoid.Died:Connect(function()
            if killer.Value then
                killer.Value = nil -- reset the killer value so that if the player dies again the last player who killed him doesn't get the cash
                killer.Value.Cash.Value += 100
            end
        end)
    end)
end)

I didn’t test it but it should work. You can tweak it as you please.

What do I put in this? I understand about what local does but what I dont understand is what to put after the hitbox = line.

Anybody else want to help me finish the problem?

Posting this again but NinjaFurfante07 has helped me a lot and I am close to finish the problem. Can anyone help him finish the problem im facing?