Game failing to load playerstats in leaderboard

Every time a player joins my game, the playerstats of someone else ingame transfer to him. if noone is in the game and a player joins then his stats load correctly.

What can i do to load the stats correctly?

What’s your script for creating the leaderstats?
Edit: This is an example of what datastore stats would look like: :arrow_down:

local DataStoreService = game:GetService("DataStoreService")
local PointsStore = DataStoreService:GetDataStore("PointsStore1")

game.Players.PlayerAdded:Connect(function(Player)
	local LeaderstatsFolder = Instance.new("Folder", Player)
	LeaderstatsFolder.Name = "leaderstats"
	local PointsValue = Instance.new("IntValue", LeaderstatsFolder)
	PointsValue.Name = "Points"
	
	local CurrentPoints = PointsStore:GetAsync(Player.UserId)
	print(CurrentPoints)
	
	if CurrentPoints == nil then
		CurrentPoints = 0
	end
	
	PointsValue.Value = CurrentPoints
	
	PointsValue.Changed:Connect(function(NewValue)
		PointsStore:SetAsync(Player.UserId, NewValue)
	end)
end)
1 Like

local DataStoreService = game:GetService(“DataStoreService”)
local ds1 = DataStoreService:GetDataStore(“CashSave”)
local ds2 = DataStoreService:GetDataStore(“KillSave”)

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”, player)
leaderstats.Name = “leaderstats”
local Cash = Instance.new(“IntValue”, leaderstats)
Cash.Name = “Cash”
local kill = Instance.new(“IntValue”, leaderstats)
kill.Name = “Kills”

wait()
Cash.Value = ds1:GetAsync(player.UserId) or 0
ds1:SetAsync(player.UserId, Cash.Value)

kill.Value = ds2:GetAsync(player.UserId) or 0
ds2:SetAsync(player.UserId, kill.Value)

local playerRemoving = function(plr)
	ds1:SetAsync(plr.UserId, Cash.Value)
	ds2:SetAsync(plr.UserId, kill.Value)
end

game.Players.PlayerRemoving:Connect(playerRemoving)end)

You don’t need to use end) It has already connected the function.

1 Like

You see you didn’t look at his entire code, he has the player removing inside of the player added. Which will definitely cause issues.

1 Like

So he must take the function and PlayerRemoving event out of the PlayerAdded event.

2 Likes

Why do you have a PlayerRemoving in a PlayerAdded? It should be outside of it, do this?

local DataStoreService = game:GetService("DataStoreService")
local ds1 = DataStoreService:GetDataStore("CashSave")
local ds2 = DataStoreService:GetDataStore("KillSave")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local Cash = Instance.new("IntValue", leaderstats)
	Cash.Name = "Cash"
	Cash.Value = ds1:GetAsync(player.UserId) or 0
	
	local kill = Instance.new("IntValue", leaderstats)
	kill.Name = "Kills"
	kill.Value = ds2:GetAsync(player.UserId) or 0	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local stats = plr.leaderstats
	local Cash = stats.Cash
	local kill = stats.Kills
	ds1:SetAsync(plr.UserId, Cash.Value)
	ds2:SetAsync(plr.UserId, kill.Value)
end)
2 Likes
local DataStoreService = game:GetService(“DataStoreService”)
local ds1 = DataStoreService:GetDataStore(“CashSave”)
local ds2 = DataStoreService:GetDataStore(“KillSave”)

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new(“Folder”, player)
	leaderstats.Name = “leaderstats”
	local Cash = Instance.new(“IntValue”, leaderstats)
	Cash.Name = “Cash”
	local kill = Instance.new(“IntValue”, leaderstats)
	kill.Name = “Kills”

	local CurrentCash = ds1:GetAsync(Player.UserId)
	print(CurrentCash)
	local CurrentKills = ds2:GetAsync(Player.UserId)
	print(CurrentKills)

	if CurrentCash == nil then
		CurrentCash = 0
	end
	if CurrentKills == nil then
		CurrentKills = 0
	end

	Cash.Value = CurrentCash
	kill.Value = CurrentKills

	Cash.Changed:Connect(function(NewValue)
		ds1:SetAsync(Player.UserId, NewValue)
		kill.Changed:Connect(function(NewValue)
			ds2:SetAsync(Player.UserId, NewValue)
		end)
	end)
end)

Try this. :arrow_up:

1 Like

I wouldn’t recommend Setting the datastore value when the value changes, it’ll lead to a lot of warnings, it’s better to set it when the player leaves or when the server shuts down

1 Like

Would also like to recommend the usage of pcall, incase roblox apis are having issues that would cause your code to stop execution. In-turn warning the player that their data didn’t load/save successfully.

1 Like

I also prefer using pcalls while loading or saving data. It will yield the function until events carry out.

1 Like

I’d also not recommend trying to save every time a value has changed, you will definitely hit rate limits depending on how frequently the value changes.

1 Like

When it comes to data stores, you can’t just save whenever you want. So you’d need to make something that only saves after a certain amount of time:

local DataStoreService = game:GetService("DataStoreService");
local Players = game:GetService("Players");

local Stats = { -- This will make it easier to add or remove stats from the game
	["Cash"] = {
		Default = 0; -- If the player has no cash, they're have 0 cash
		Key = "cash"; -- The data store key
		ValueType = "Int"; -- The type of data being stored
	};
	["Kills"] = {
		Default = 0;
		Key = "kills";
		ValueType = "Int";
	};
};

Players.PlayerAdded:Connect(function(player)
	local ls = Instance.new("Folder", player);
	ls.Name = "leaderstats";
	
	for statName, statSettings in pairs(Stats) do
		local newStat = Instance.new(statSettings.ValueType .. "Value", ls);
		newStat.Name = statName;
		
		local playerId = "Player_" .. player.UserId;
		local dataStore = DataStoreService:GetDataStore(statSettings.Key);

		newStat.Value = dataStore:GetAsync(playerId) or statSettings.Default;

		coroutine.wrap(function()
			while wait(30) do
				dataStore:SetAsync(playerId, newStat.Value);
			end;
		end)();
	end;
end);

Further Research:

1 Like