Here's the script:

local DataBaseStore = game:GetService("DataStoreService"):GetDataStore("AllTheStatsAreHere")
local Statsholder = script.Stats
local players = game:GetService("Players")


players.PlayerAdded:Connect(function(NewPlayer)
	
	local key = "Player-ID:" .. NewPlayer.UserId
	local GetSave = DataBaseStore:GetAsync(key)
	
	local DataBaseFolder = Instance.new("Folder", NewPlayer)
	DataBaseFolder.Name = "PlayerStats"
	
	for i, Stats in pairs(Statsholder:GetChildren()) do
		local NewStats = Instance.new(Stats.ClassName)
		NewStats.Name = Stats.Name
		NewStats.Value = Stats.Value
		NewStats.Parent = DataBaseFolder
	end
	
	if GetSave then
		
		for i, Stats in pairs(script.Stats:GetChildren()) do
			
			DataBaseFolder[Stats.Name].Value = GetSave[i]
			print(i, Stats.Name, Stats.Value)
			
			end
			
	else -- Save Stats
		
		local StatsToSave = {}
			
			for i, Stats in pairs(DataBaseFolder:GetChildren()) do
				
				table.insert(StatsToSave, Stats.Value)
				print(i, Stats.Name, Stats.Value)
				
			end
			
			DataBaseStore:SetAsync(key, StatsToSave)
			
	
		end
		
		while wait(15) do  --Autosave
			
			local StatsToSave = {}
			
			for i, Stats in pairs(DataBaseFolder:GetChildren()) do
				
				table.insert(StatsToSave, Stats.Value)
				print(i, Stats.Name, Stats.Value)
				
			end
			
			DataBaseStore:SetAsync(key, StatsToSave)
			
		end
		
		players.PlayerRemoving:Connect(function(OldPlayer)
			
			
			local key = "Player-ID:" .. OldPlayer.UserId
			
			local StatsToSave = {}
			
			for i, Stats in pairs(DataBaseFolder:GetChildren()) do
				
				table.insert(StatsToSave, Stats.Value)
				print(i, Stats.Name, Stats.Value)
				
			end
			
			DataBaseStore:SetAsync(key, StatsToSave)
			
		end)
		
		
end)

So technically the script should save those stats as the player is leaving since the output displays no errors. It does save every 15 seconds, but doesn't when the player leaves. Any ideas how could this get fixed?

3 comments

0

most of the time, its not the datastores fault, its a localscript controlling the players data which wont be registered by the server

0

So how am i supposed to fix it?

0

Also this script isn't local, it's a normal script inside the serverscriptservice