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

Alright. Bingo. I was being stupid. Delete your leaderstats script and your datastore script. Delete the script in StarterCharacterScripts too.

Make a new regular script in ServerScriptService.

Then paste this code inside:

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

Now try changing it manually in the server and check if it works.

It has the same problem as the last one. The leaderstats doesn’t apear…

Are you positive that both the DataStore script and your previous leaderstats scripts is deleted? And that the script @madonchik123 told you to add in StarterCharacterScripts is deleted as well?

It works perfectly fine for me.

Also, I see that its working in your screenshot.

StarterCharacterScripts goes into character not player

I’m aware of that.

In the screenshoot that’s the script that I created. And yes, al the scripts are gone. I will try it in a baseplate to see if it’s works for me too

I changed the value of the kills and it worked. But it’s manually.

Wait wait wait! I found that in you script at the final the ends were wrong so I fixed them and now the leaderstats is apearing but when I kill an NPC from tooblox the kills value doesn’t go up. But when I colelct a coin the coins value are going up. Is that because I use an NPC and not a real player??

1 Like

Correct. It needs to be a real player in order for it to work.

But does the script work now?

Wait I will test it with an real player and I will text you if will work!

Sad news. The script doesn’t work… :((

which part? The leaderstats or the leaderstats on kill?

The kills are not working… The leaderstats on kill

I believed I FINALLY fixed it:

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
    Stars.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()
			local tag = character.Humanoid:FindFirstChild("creator")
		        if tag ~= nil then
			local creator = tag.Value

			local leaderstats = player:WaitForChild("leaderstats")
			leaderstats.Kills.Value = 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)

If this doesn’t work idk what will

do you know how scripting works…?
what is your problem, may you elaborate more?

should be turned into

Stars.Value = 0

just a correction

Hi! Just wanted to point this out. It’s certainly not because he doesn’t set the value’s to 0 :smiley:
The value for a new IntValue is default set to 0, so no need to do that at least. :slight_smile:

1 Like

How can i find who damaged the Player/NPC Last reply in this thread solves your problem! :slight_smile: