Hey. I'm trying to make a leaderboard that saves when the player quits (and load) using DataStores. It works with 1 value, but when i try to put the table with 4 value into the data store it doesnt load up when i join.

Script in ServerScriptService:

local DataStoreService=game:GetService("DataStoreService")
local DataStore=DataStoreService:GetDataStore("Player_Data")

--[[
	A player has joined the game. Load/Create their data.
--]]
game.Players.PlayerAdded:connect(function(Player)
	
	local PlayerData; --Will hold the player's data when fetched from the datastore.	
	
	--[[
		Set up the player's leaderstats and add them to the ROBLOX leaderboard.
	--]]
	local Stats=Instance.new('Model')
	Stats.Name="leaderstats"
	
	local Cash=Instance.new('IntValue',Stats)
	Cash.Name="Cash"
	
	local Points=Instance.new('IntValue',Stats)
	Points.Name="Points"
	
	local Ammo=Instance.new('IntValue',Stats)
	Ammo.Name="Ammo"
	
	local Damage=Instance.new('IntValue',Stats)
	Damage.Name="Damage"
	
	Stats.Parent=Player
	
	--[[
		We are fetching the player's data, if it exists.
		If it does not exist, we will create new data for the player as they
		must logically be new to the game.
	--]]
	local CanSave=Instance.new('BoolValue',Player) --This is used to determine whether or not the 
	                                               --player's data can be saved when they leave.
	CanSave.Name="CanSaveData"
	CanSave.Value=true
	
	local DataFetchSuccess,ErrorMessage=pcall(function() --We are wrapping the datastore request in a protected function, as web requests can error. 
	                                                     --Safely handling datastore errors can prevent player data corruption.
	PlayerData=DataStore:GetAsync(tostring(Player.UserId))
	end)
	
	if DataFetchSuccess then --The datastore GET request was successful!
		
		if PlayerData~=nil then --The player's data exists, they have played this game before. Put their data into the leaderboard.
			Player.leaderstats.Cash.Value=PlayerData[1] or 0
			Player.leaderstats.Points.Value=PlayerData[2] or 0
			Player.leaderstats.Ammo.Value=PlayerData[3] or 0
			Player.leaderstats.Cash.Value=PlayerData[4] or 0
		else --The player's data does not exist, they're new to the game. Create their data on the leaderboard.
			Player.leaderstats.Cash.Value=100 --Players start out with 100 money in this example.
			Player.leaderstats.Points.Value=19
			Player.leaderstats.Ammo.Value=5
			Player.leaderstats.Cash.Value=10
		end
		
	else --The GET request failed, datastores could be down.
		
		--[[
			There are many ways to handle datastore failures. In this case, we will kick the player, 
			letting them know their data failed to load. 
			Because their data wasn't loaded, we won't save their data when they leave.
		]]--
	
		Player.CanSaveData.Value=false
		Player:Kick("Your data failed to load! Please rejoin.")	
			
	end
  
	end)

--[[
	A player is leaving the game. Save their data.
--]]
game.Players.PlayerRemoving:connect(function(Player)
	
	if Player.CanSaveData.Value==false then return end --Player data can't be saved, so do nothing.
	
	local PlayerData={
		Player.leaderstats.Cash.Value,
		Player.leaderstats.Points.Value,
		Player.leaderstats.Ammo.Value,
		Player.leaderstats.Damage.Value
	}
	
	local DataWriteSuccess,ErrorMessage=pcall(function() --Once again, we are safely calling a web request. If it fails, we can safely handle it.
		DataStore:SetAsync(tostring(Player.UserId),{
			Player.leaderstats.Cash.Value,
			Player.leaderstats.Points.Value,
			Player.leaderstats.Ammo.Value,
			Player.leaderstats.Damage.Value
		})
	end)	
	
	if not DataWriteSuccess then --Uh oh, player data didnt' save. Handle this error.
		--We will attempt to save the player's data 5 more times. If it fails after 5 times, 
		--we will abort the save and the player's data will not be changed in the datastores.
		
		local Retry_Count=0
		
		while Retry_Count<6 do
			wait(60) --Wait 1 minute between each retry
			local Succeded,Error=pcall(function()
				DataStore:SetAsync(tostring(Player.UserId),{
					Player.leaderstats.Cash.Value,
					Player.leaderstats.Points.Value,
					Player.leaderstats.Ammo.Value,
					Player.leaderstats.Damage.Value
				})
			end)
			if Succeded then break end --HURRAY, DATA SAVED!
			Retry_Count=Retry_Count+1
		end
		
	end
	
end)

Any help would be appreciated!

1 comment

0

i tried to make it myself but it did not work... so i tried this script that someone posted