How to add a leaderstat when killing a zombie?

I am making a zombie game and I am trying to add a leaderstat thing when killing a zombie and this is the following script, but it failed.

game.Zombie.Death:addLeaderstat

Zombie.Died:addLeaderstat(“Cash”)

When Zombie.Death = true do

addLeaderstat(“Cash”)

Cash.Value = + 1

end)
2 Likes

This should help, make a script in your zombie and paste it. (PS: Leaderstats must be named “Cash”)

local Humanoid = script.Parent.Zombie
function PwntX_X()
local tag = Humanoid:findFirstChild(“creator”)
if tag ~= nil then
if tag.Value ~= nil then
local Leaderstats = tag.Value:findFirstChild(“leaderstats”)
if Leaderstats ~= nil then
Leaderstats.Cash.Value = Leaderstats.Cash.Value + 5 --Change Money to the stat that is increased.
wait(0.1)
script:remove()
end
end
end
end
Humanoid.Died:connect(PwntX_X)

2 Likes

Take a look at the script in the model and see if you can understand it Kill npc for cash - Roblox

Use it with Linked Sword - Roblox
In roblox studio because the script inside the sword creates a tag in the npc to say “Localplayer damaged the npc” and the script in the npc says “Ah a tag when I die I will reward this guy with money” (Look for the money kill script not the pathfinding or ai)

2 Likes

I didn’t see the leaderstat on the leaderboard. Did the script create a leaderstat or not because I am not noticing the cash on the leaderboard. I don’t know if it is supposed to create a leaderstat.

1 Like

Using whatever weapon you want, you could basically add that code in there. Ex. A sword. If your sword hitbox comes in contact with a zombie humanoid, take 15 hp away. Also check if the humanoid health is below 15, so you can manually make the zombie die and give cash to the player. LocalScript would be inside the sword.

First, you need to add leaderstat. Next, name it “Cash” or change the cash text from script to your money name. (Can’t have spaces)

The humanoid name is “Zombie”. There was only the humanoid named “humanoid” in that script that @Kacper03050 showed me. Its hard doing this because the humanoid that is named “humanoid” is for the players. The player humanoid is named “humanoid”. The zombies humanoid is named “Zombie”.

You need to name it “Humanoid”.

Try that.

local Humanoid = script.Parent.Zombie
function PwntX_X()
local tag = Humanoid:findFirstChild(“creator”)
if tag ~= nil then
if tag.Value ~= nil then
local Leaderstats = tag.Value:findFirstChild(“leaderstats”)
if Leaderstats ~= nil then
Leaderstats.Cash.Value = Leaderstats.Cash.Value + 5 --Change Money to the stat that is increased.
wait(0.1)
script:remove()
end
end
end
end
Humanoid.Died:connect(PwntX_X)

local DataStoreService = game:GetService(“DataStoreService”)
local DataStore = DataStoreService:GetDataStore(“CashStats”) – Change this with a different name.

game.Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new(“Folder”, Player)
Leaderstats.Name = “leaderstats”
local Currency = Instance.new(“IntValue”, Leaderstats)
Currency.Name = “Cash” – Change “Money” with your currency.
Currency.Value = 0

local Data = DataStore:GetAsync(Player.UserId)
if Data then
    Currency.Value = Data -- Change this if you have added more currencies.
end

end)

the script.Parent.Zombie doesn’t affect the zombie humanoid, it just doesn’t give the player any leaderstat.

Where exactly are you getting the method/actions? Where did you get “when thing_that_happens_here do”? These aren’t really actual built-in functions.

What you should do is make it so that when someone damages a zombie with a weapon, it checks for a StringValue containing their name - if there is none, then a StringValue is then added onto the zombie’s Humanoid or wherever. But if there is one then they will instead replace the value of that StringValue with their name.

You can then call the Humanoid.Died event on the zombie’s humanoid, then search for the StringValue. After that, you can search the Players service for the player name, and then look for their leaderstats.

For the zombie:

local Players = game:GetService("Players");
local zombie = script.Parent;
local humanoid = zombie.Humanoid;

humanoid.Died:Connect(function()
    local Creator = humanoid:FindFirstChild("Creator"); --replace creator with the stringvalue's name
    if Creator then
        local killer = Players:FindFirstChild(Creator.Value);
        if killer then
            local leaderstats = killer:FindFirstChild("leaderstats");
            leaderstats.Cash.Value += 1
        end
    end
end)

Since I don’t really know what kind of weapon the game has, I’m afraid you’ll have to figure out on your own on how to give the zombie’s humanoid the Creator object once a player damages the zombie; you can try looking at the LinkedSword at free models, it gives out a neat example

2 Likes

Hey, I’ve Been Trying Something Like This With A Npc in my game I got a string value that puts the name in. Your script doesn’t seem to work for some reason.

Are you sure the StringValue is being added through the server and not on the client?

1 Like