How to fix my leaderstats

Hey guys today i was scripting and i saw a problem with my leaderstats i cant find out the problem why my points and wins not showing up? can you guys help me Capture and this is my script: local ds = game:Getservice(“DataStoreService”)
local stats = ds:GetDataStore(“playerStats_00”)

function getplayerData(player)
local success, data = false, nil
repeat
success = pcall(function()
data = stats:GetAsync(player.UserId)
end)
until success

player.DataLoaded.Value = true

return data

end

function savePlayerData(player)
if player.DataLoaded.Value then
local data = {}
for i,v in pairs(player.leaderstats:GetChildren()) do
table.insert(data, v.Value)
end
pcall(function()
stats:SetAsync(player.UserId, data)
end)
end
end

function giveStats(player)
local leaderstats = Instance.new(“Folder”, player)
leaderstats.Name = “leaderstats”

local Points = Instance.new("IntValue", leaderstats)
Points.Name = "Points"

local Wins = Instance.new("IntValue", leaderstats)
Wins.Name = "Wins"

local loaded = Instance.new("BoolValue", player)
loaded.Name = "DataLoaded"

local data = getplayerData(player)
if data then
	Points.Value = 0
	Wins.Value = 0
end

end

game.Players.PlayersAdded:Connect(giveStats)
game.Players.PlayerRemoving:Connect(savePlayerData)

while wait(30) do
for _, player in pairs(game.Players:GetPlayers()) do
savePlayerData(player)
end
end

Please format your code properly with three backticks.

2 Likes

There’s your problem, PlayersAdded is a misspelling of PlayerAdded.


P.S: The data is never used here, just sets the values to 0:

Also, I’d advise against the usage of the Parent argument in Instance.new because:

Hmm, its called PlayerAdded event not PlayersAdded

this should work

local DataStoreService = game:GetService("DataStoreService")
local stats = DataStoreService:GetDataStore("playerStats_00")

function getplayerData(player)
	local success, data = false, nil
	local tries = 3
	repeat
		success = pcall(function()
			data = stats:GetAsync(player.UserId)
		end)
		if not success then
			wait(1)
		end
		tries -= 1
	until tries == 0 or success

	player.DataLoaded.Value = true

	if success then
		if data then
			-- Data exists for this player
			return data
		else
			-- Data store is working, but no current data for this player
			return
		end
	else
		warn("Cannot access data store for player!")
	end
end

function savePlayerData(player)
	if player.DataLoaded.Value then
		local data = {}
		for _, v in pairs(player.leaderstats:GetChildren()) do
			table.insert(data, v.Value)
		end

		local tries = 3
		local success
		repeat
			success = pcall(function()
				stats:SetAsync(player.UserId, data)
			end)
			if not success then
				wait(1)
			end
			tries -= 1
		until tries == 0 or success
		if not success then
			warn("Cannot save data for player!")
		end
	end
end

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

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

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

	local loaded = Instance.new("BoolValue")
	loaded.Name = "DataLoaded"
	loaded.Parent = player

	local data = getplayerData(player)
	if data then
		Points.Value = 0
		Wins.Value = 0
	end
end

game:GetService("Players").PlayerAdded:Connect(giveStats)
game:GetService("Players").PlayerRemoving:Connect(savePlayerData)

while true do
	wait(30)
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		savePlayerData(player)
	end
end

This basically only had two syntax errors, but I changed other parts following good practices.

well, that’s not exactly the reason

this is actually a good practice for reasons of flexibility.

ty for telling me i will work on my