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