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

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: