Help with my leaderboard pls

Im trying to figure out whats wrong with the script i got help to do…
The death and kills doesnt count.

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats

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

local deaths = Instance.new("IntValue")
deaths.Name = "Deaths"
deaths.Parent = leaderstats

while wait(1) do
	player:WaitForChild("leaderstats"):WaitForChild("Coins").Value += 1
end

end)

plr.CharacterAdded:connect(function(char)
local humanoid
repeat
humanoid = char:FindFirstChild(“humanoid”)
wait()
until humanoid
humanoid.Died:connect(function()
deaths.Value = deaths.Value + 1
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)

I think theres a typo when you tried to define your Humanoid. Its suppose to be a capital H.

yes, and you wrote connect() instead of Connect()

Press the menu option that looks like </>, paste your code inside the two … lines

like this:

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local coins = Instance.new("IntValue")
    coins.Name = "Coins"
    coins.Parent = leaderstats

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

    local deaths = Instance.new("IntValue")
    deaths.Name = "Deaths"
    deaths.Parent = leaderstats

    while wait(1) do
        coins.Value += 1
    end

    player.CharacterAdded:Connect(function(char)
        local humanoid = char:WaitForChild("Humanoid")
        humanoid.Died:Connect(function()
            deaths.Value += 1
            local tag = humanoid:FindFirstChild("creator")
            if tag then
                local killer = tag.Value
                if killer then
                    killer.leaderstats.Kills.Value += 1
                end
            end
        end)
    end)
end)

This wasn’t too bad but, you did have a few errors. Should be all fixed up now. GL!
A few syntax errors and a logic error.

1 Like

sorry, but it still doesnt work
i have this code under the leaderstats…

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetDataStore("SaveLeaderstats")

Players.PlayerAdded:Connect(function(player)
	local Data = nil
	local success, errormessage = pcall(function()
		Data = Saver:GetAsync(tostring(player.UserId))
		print("Successfully loaded "..tostring(player.Name)..tostring("'s data."))
	end)

	if success then
		if Data then
			for i, v in pairs(Data) do
				player:WaitForChild("leaderstats"):WaitForChild(i).Value = v
			end
		end
	else
		error(errormessage)
	end
end)

local function Save(player)
	local SavedData = {}
	for _, v in pairs(player.leaderstats:GetChildren()) do
		SavedData[v.Name] = v.Value
	end

	local success, errormessage = pcall(function()
		Saver:SetAsync(tostring(player.UserId), SavedData)
		print("Successfully got "..tostring(player.Name)..tostring("'s data."))
	end)
	if not success then
		error(errormessage)
	end
end

Players.PlayerRemoving:Connect(Save)

game:BindToClose(function()
	for _, v in pairs(Players:GetPlayers()) do
		Save(v)
	end
end)
1 Like

i really really have no idea what im doing… i google and watch yt to try to understand… but this mom seems to be a lost case :wink:

Hey sorry I took so long to get back to you had a few things to get done. I know how frustrating datastores can be when they don’t work. In this case I see a few maybe problems. The major one is you may be trying to work with leaderstats before it exists. So let’s give this a 2nd attempt.

It also seems like I’m missing parts of the code here. What happen to Coins, Kills and Deaths.
I take it you are now just looking for a general outline to work from …

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetDataStore("SaveLeaderstats")

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Data = nil
	local success, errormessage = pcall(function()
		Data = Saver:GetAsync(tostring(player.UserId))
		print("Successfully loaded " .. tostring(player.Name) .. "'s data.")
	end) if not success then warn(errormessage) end

	if success then
		if Data then
			for i, v in pairs(Data) do
				local stat = Instance.new("IntValue")
				stat.Name = i
				stat.Value = v
				stat.Parent = leaderstats
			end
		end
	end
end)

local function Save(player)
	local SavedData = {}
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		for _, v in pairs(leaderstats:GetChildren()) do
			SavedData[v.Name] = v.Value
		end
	end

	local success, errormessage = pcall(function()
		Saver:SetAsync(tostring(player.UserId), SavedData)
		print("Successfully saved " .. tostring(player.Name) .. "'s data.")
	end) if not success then warn(errormessage) end
end

Players.PlayerRemoving:Connect(Save)

game:BindToClose(function()
	for _, player in pairs(Players:GetPlayers()) do
		Save(player)
	end	task.wait(5) -- keep this here, it is a save all pause.
end)

I changed my mind and just created leaderstats after PlayerAdded.
Changed my mind about a few things again, because I’m super picky.
I don’t see how this one can fail and if it does it should tell you why.
I don’t like any printing in the pcall, so I added an error warning.
With that, I guess it’s ok …

looks like this
it made it look like this :slight_smile:

1 Like

That looks pretty good. Really like the choosing category.

it doesnt have coins, death and kills on it :slight_smile:

Watch this one and then watch every video this guy made, twice … :rofl:
Roblox DataStore Tutorial - Datastores and Saving Data - Scripting Tutorial
In my opinion this guy here, AlvinBlox, is one of the best.

hahaha il try that… but i really dont think i will ever understand it tbh… i just want death and kills to count on my regular script…haha

1 Like

Then why did you take it out? Don’t let these datastores overwhelm you.
They are actually pretty easy to work with once you get down the basic outline.
One of them things you only really need to learn once. Then you have a working version
to use as a reference.

You can add in the death and kills right under the line: leaderstats.Parent = player
Just like you had it before.

i just tested the code u created :slight_smile: but maybe i missunderstood how u meant…

you need to make sure you have created a leaderstats folder, and that the player object is referenced correctly (in my opinion)
The Script:

local player = game.Players.LocalPlayer -- Oder eine andere Referenzierung zu einem Spieler

local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then
    leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
end

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

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

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

while wait(1) do
    local leaderstats = player:WaitForChild("leaderstats")
    local coins = leaderstats:WaitForChild("Coins")
    coins.Value = coins.Value + 1
end

If it wount Work pls Tell me so i can fix it If there IS a error

like this now
now it looks like this…

1 Like

So does it Work Like you want it?

This is the code im trying to fix. so kills and death work when one player shoot someone else. the leaderboard works. +1 coin works. its ony the death and kills when someone kill/die that doesnt work…

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local coins = Instance.new("IntValue")
    coins.Name = "Coins"
    coins.Parent = leaderstats

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

    local deaths = Instance.new("IntValue")
    deaths.Name = "Deaths"
    deaths.Parent = leaderstats

    while wait(1) do
        coins.Value += 1
    end

    player.CharacterAdded:Connect(function(char)
        local humanoid = char:WaitForChild("Humanoid")
        humanoid.Died:Connect(function()
            deaths.Value += 1
            local tag = humanoid:FindFirstChild("creator")
            if tag then
                local killer = tag.Value
                if killer then
                    killer.leaderstats.Kills.Value += 1
                end
            end
        end)
    end)
end)

no :slight_smile: i have coin, kills and death on my lb :slight_smile:

i mean… it doesnt add a point when someone get killed or when someone kills someone