Leaderstats bug

Leaderstats are for some reason okay in studio but in the actual game it’s in disarray please help.

Correct (In Studio)

Incorrect (In Game)

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("Data")

local function saveData(player)

	local tableToSave = {
		player.leaderstats.wins.Value,
		player.leaderstats.ties.Value,
		player.leaderstats.losses.Value,
		player.leaderstats.streak.Value,
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave)
	end)

	if success then
		print("Data has been saved!")
	else
		print("Data hasn't been saved!")
		warn(err)		
	end
end

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local streak = Instance.new("NumberValue")
	streak.Name = "streak"
	streak.Parent = leaderstats
	
	local wins = Instance.new("NumberValue")
	wins.Name = "wins"
	wins.Parent = leaderstats
	
	local losses = Instance.new("NumberValue")
	losses.Name = "losses"
	losses.Parent = leaderstats

	local ties = Instance.new("NumberValue")
	ties.Name = "ties"
	ties.Parent = leaderstats

	
	local data
	local success, err = pcall(function()
		data = dataStore:GetAsync(player.UserId)
	end)

	if success and data then

		wins.Value = data[1]
		ties.Value = data[2]
		losses.Value = data[3]
		streak.Value = data[4]

	else
		print("The player has no data!")
	end

end)
game.Players.PlayerRemoving:Connect(function(player)
	saveData(player)
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		saveData(player)
	end
end)

maybe you can add a task.wait() before the numbervalue is created

This didn’t work but I found out something weird, when I delete the streak value entirely from being made everything goes back to normal.

Alright, I increased the task.wait() from just being 0 to being 1 and it seemed to fix it, weird though

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.