DataStore | Problem Saving

Hello Developers,

I have a leaderboard and the data store saves and loads all data correctly. I have a script that is adding 1 point every 30 seconds to a player, its in ServerScriptService and its adding the points correctly its just not saving the data to the DataStore, Here are my scripts:

Leaderboard Script

local DS = game:GetService("DataStoreService")
local PointsStore = DS:GetDataStore("Points")
local playerStats = {}
group = script.GroupID.Value
default = script.DefaultRank.Value

game.Players.PlayerAdded:connect(function(player)
	local leaderstats = Instance.new("Model", player)
	leaderstats.Name = "leaderstats"
	local grouprank = Instance.new("StringValue", leaderstats)
	grouprank.Name = "Rank"
	local rank = player:GetRoleInGroup(group)
	if rank ~= "Guest" then
		grouprank.Value = rank
	else
		grouprank.Value = default
	end

	local points = Instance.new("IntValue", leaderstats)
	points.Name = "Points"
	playerStats[player] = leaderstats
	
	local data
	local success, Error = pcall(function()
		data = PointsStore:GetAsync(player.UserId.."-Points")
	end)
	
	if success and data then
		points.Value = data
	else
		warn(Error)
	end

	game.Players.PlayerRemoving:Connect(function(plr)
   -- define data
   local data
   -- to prevent errors or fails if datastoreervice is down or not
   local success,err = pcall(function()
    -- ser the async so we could get it back when he joins again
    data = PointsStore:SetAsync(plr.UserId.."-Points", plr.points.Value)
   end)
   -- check if it saved or not if not its gonna print an error
		   if success then 
			print("Successfully saved") 
		else 
		print(error) end
end)
end)

Add points script

game.Players.PlayerAdded:Connect(function(plr)
	while wait(30) do
 	  for i,plr in pairs(game.Players:GetPlayers()) do
     	 if plr:IsInGroup(6792664) and plr:GetRankInGroup(6792664) >= 5 then
    	    plr.leaderstats.Points.Value += 1
			end
		end
	end
end)

Does anyone know how I could fix this?

1 Like

You are incorrectly printing an error. Also, does you didn’t define leaderstats when saving.

I fixed the printing part and I do not need to define “leaderstats” in whole because I only want it to save the points value.

The PlayerRemoving must be outside of the PlayerAdded. As for accessing the points value, just do Player.leaderstats.Points.Value.

Solved, simplified my leaderboard script.

2 Likes