I'm trying to make a leaderstats with kills but it doesn't work! Please help me

Hello developers,
These days I was bored and I decided to create a competitive game but the only problem is that I can’t create a leaderstats with kills. So I want to make a leaderstats with kills and whenever you kill somebody with a tool the kills would increase by +1. I have this code right here but it doesn’t work, the output hasn’t any error or something like that.

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = plr
	
	local strength = Instance.new("IntValue")
	strength.Name = "Strength"
	strength.Parent = stats
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = stats
	
	local star = Instance.new("IntValue")
	star.Name = "Stars"
	star.Parent = stats
	
	local kill = Instance.new("IntValue")
	kill.Name = "Kills"
	kill.Parent = stats
	
	plr.CharacterAdded:connect(function(char)
	local humanoid = char:FindFirstChild("Humanoid")
	local tag = humanoid:FindFirstChild("creator") 
	if tag then  
		local killer = tag.Value  
		if killer then   
			killer.leaderstats.Kills.Value = killer.leaderstats.Kills.Value + 1
		end
	end
 end)
	
end)

Can someone please help me with this problem?
Thanks,
Daniel

I think its because you’re not setting a value for the kills. Try this:

local Players = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = plr
	
	local strength = Instance.new("IntValue")
	strength.Name = "Strength"
	strength.Parent = stats
        strength.Value = 0
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = stats
        coins.Value = 0
	
	local star = Instance.new("IntValue")
	star.Name = "Stars"
	star.Parent = stats
	stars.Value = 0

	local kill = Instance.new("IntValue")
	kill.Name = "Kills"
	kill.Parent = stats
        kill.Value = 0
	
	plr.CharacterAdded:Connect(function(Character)
               Character.Humanoid.Died:Connect(function(Died)
	local creator = Character.Humanoid:FindFirstChild("creator")
	local leaderstats = creator.Value:FindFirstChild("leaderstats")
	if creator ~= nil and creator.Value ~= nil then   
			leaderstats.Kills.Value = leaderstats.Kills.Value + 1
end
	end)
 end)
	
end)
1 Like

underneath

put:

kill.Value = 0

Yeah. Don’t forget the Humanoid.Died function as well.

I will try this, I hope this works. Thank you!

This doesn’t work. I don’t know what’s wrong…

Hmmmm. Are you trying to put a DataStore with the kill leaderstats (by datastore I mean when you leave and rejoin it will save the kill count)

Yes, I have a datastore script for the leaderstats.

Alright. Disable the DataStore script that you have and use the script below, which has both the leaderstats and DataStore in one script:

local Players = game:GetService("Players")
local TestService = game:GetService("TestService")
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreValues") --Name the DataStore whatever you want

Players.PlayerAdded:Connect(function(player)

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local Strength = Instance.new("IntValue")
    Strength.Name = "Strength"
    Strength.Parent = leaderstats
    Strength.Value = 0
 
    local Coins = Instance.new("IntValue")
    Coins.Name = "Coins"
    Coins.Parent = leaderstats
    Coins.Value = 0

     local Stars = Instance.new("IntValue")
    Stars.Name = "Stars"
    Stars.Parent = leaderstats
    Strength.Value = 0

    local Kills = Instance.new("IntValue")
    Kills.Name = "Kills"
    Kills.Parent = leaderstats
    Kills.Value = 0

	local value1Data = Strength
        local value2Data = Coins
        local value3Data = Stars
        local value4Data = Kills
player.CharacterAdded:Connect(function(Character)
		Character.Humanoid.Died:Connect(function(Died)
			local creator = Character.Humanoid:FindFirstChild("creator")
			local leaderstats = creator.Value:FindFirstChild("leaderstats")
			if creator ~= nil and creator.Value~= nil then
                                leaderstats.Kills.Value += 1
			end
		end)
	end)

	local s, e = pcall(function()
		value1Data = DataStore:GetAsync(player.UserId.."-Value1") or 0 --check if they have data, if not it'll be "0"
       value2Data = DataStore:GetAsync(player.UserId.."-Value2") or 0
       value3Data = DataStore:GetAsync(player.UserId.."-Value3") or 0
       value4Data = DataStore:GetAsync(player.UserId.."-Value4") or 0
	end)

	if s then
		Strength.Value = value1Data --setting data if its success
                Coins.Value = value2Data
                Stars.Value = value3Data
                Kills.Value = value4Data
	else
		TestService:Error(e)  --if not success then we error it to the console
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
local s, e = pcall(function()
	DataStore:SetAsync(player.UserId.."-Value1", player.leaderstats.Strength.Value) --setting data
       DataStore:SetAsync(player.UserId.."-Value2", player.leaderstats.Coins.Value)
	DataStore:SetAsync(player.UserId.."-Value3", player.leaderstats.Stars.Value)
        DataStore:SetAsync(player.UserId.."-Value4", player.leaderstats.Kills.Value)
end)
	if not s then TestService:Error(e) 
	end
end)

game:BindToClose(function(player)
      if not RunService:IsStudio() then
          local s, e = pcall(function()
         DataStore:SetAsync(player.UserId.."-Value1", player.leaderstats.Strength.Value) --setting data
       DataStore:SetAsync(player.UserId.."-Value2", player.leaderstats.Coins.Value)
	DataStore:SetAsync(player.UserId.."-Value3", player.leaderstats.Stars.Value)
        DataStore:SetAsync(player.UserId.."-Value4", player.leaderstats.Kills.Value)
	end)
	if not s then TestService:Error(e) 
	end

       end)
end)
1 Like

i don’t think leaderstats for kill will work you should make it other way where you will add script to all characters that checks if they got killed and after that it will give kill to the killer I was already making it

So I need to delete the datastore script I have and the leaderstats one? Or just the datastore script?

Try adding prints all along the script, check where it stops printing and let me know.

here put script in StarterPlayer.StarterCharacterScripts

local Humanoid = script.Parent.Humanoid
function Dead()
	local tag = Humanoid:FindFirstChild("creator")
	print(tag)
	if tag ~= nil then
		print(tag.Value)
		if tag.Value ~= nil then
			local leaderstatsname = "your leaderstat name"
			local leaderstats = tag.Value:FindFirstChild("leaderstats")
			print(leaderstats)
			if leaderstats ~= nil then
				local value = leaderstats[leaderstatsname]
				value.Value = value + 1
				
			end
		end
	end
end

Humanoid.Died:Connect(Dead)

I will try this! I hope it will work. Thanks!

First you Disable the DataStore script, then Delete the leaderstats script, then create a new server script in serverscriptservice, and then paste the code above into the script.

I did this and it doesn’t show me the leaderstats…

Never handle leaderstats on the client. Exploiters can easily modify it to get infinite kills. Also, server scripts can’t be put in StarterCharacterScripts iirc

use your old one and just do thing that i said

by saying server script i mean just a basic script not a local script

You can’t put regular scripts inside StarterCharacterScripts.