Game doesn't save currency amount

This script is supposed to save the amount of Displays (currency) that the player has after they left the game. There are 3 scripts, but the ServerScriptService one is the one that causes the problem.

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("StoreKey")

local playerData = script.PlayerData

local function SaveData(player)
	local data = {}
	for _, folder in pairs(player:GetChildren())  do
		if playerData:FindFirstChild(folder.Name) then
			if playerData[folder.Name]:GetAttribute("SaveChildren") == true  then
				data[folder.Name] = {}
				if playerData[folder.Name]:GetAttribute("SaveChildrenValues") == true  then
					for _, child in pairs(folder:GetChildren()) do
						if not child:GetAttribute("DonNotSaveValue") then
							table.insert(data[folder.Name], {child.Name, child.Value,  child.ClassName})
							
						
						end
					end
				else 
					for _, child in pairs(folder:GetChildren()) do
						if not child:GetAttribute("DoNotSaveValue") then 
							table.insert(data[folder.Name], {child.Name, child.ClassName})
						end
					end
				end
			end
		end
	end
	local succes, errorMsg = pcall(function()
		dataStore:SetAsync(player.UserId.."Key", data)
	end)
	
	if succes then
		print("Data Saved")
	else 
		warn(errorMsg)
	end
end

local function LoadData(player)
	for _, v in pairs(playerData:GetChildren()) do 
		v:Clone().Parent = player
	end
	local data
	local succes, errorMsg = pcall(function()
		data = dataStore:GetAsync(player.UserId.."Key")
	end)
	if errorMsg then --Save feature for error join
		warn(errorMsg)
		player:Kick("Could not load your data please try again")
	end
	if data then    --checking how much data there is.
		for i,v in pairs(data) do 
			if #v > 0 then 
				for x, c in pairs(v) do 
					if player[tostring(i)]:FindFirstChild(c[1]) then
						player[tostring(i)]:FindFirstChild(c[1]).Value = c[2]
					else 
						local value
						if c[3] == nil then 
							value = Instance.new(tostring(c[2]))
						else 
							value = Instance.new(tostring(c[3]))
						end
						value.Name = c[1]
						value.Value = c[2]
						value.Parent = player[tostring(i)]
					end
				end
			end
		end
	end
end

local function PlayerRemoving(player)
	SaveData(player)
end

players.PlayerAdded:Connect(LoadData)
players.PlayerRemoving:Connect(PlayerRemoving)
game:BindToClose(function()
	for _, player in pairs(players:GetPlayers()) do
		coroutine.wrap(SaveData)(player)
	end 
end)

There’s not much else to say. The script for when the player collects the currency is here. It is located in the Prompt which is in the currency’s model.

local Prompt = script.Parent
local Att = Prompt.Parent
local Center = Att.Parent
local Display = Center.Parent
local Sound = Prompt.CollectSound

function onPromptTriggered(Player)
	local PlayerGUI = Player.PlayerGui
	local Values = PlayerGUI:WaitForChild("Values")
	local Displays = Values.Displays
	local DisplayAmount = Displays.DisplayAmount
	Sound:Play()
	Display:Destroy()
	DisplayAmount.Value = DisplayAmount.Value + 1
end

Prompt.Triggered:Connect(onPromptTriggered)

Is the second code sample a localscript or serverscript?

The second code sample is a server script.

The error is that the script is not saving the amount of Displays the player has after they leave the game.

The problem is that the script is not saving the data when the player leaves the game. To fix this, you would need to add code to the PlayerRemoving function in the ServerScriptService script to save the player data. You can do this by adding the SaveData(player) function call to the PlayerRemoving function. You would also need to add the game:BindToClose() function to the end of the script to make sure the player data is saved when the game closes.

Isn’t that what i’ve already done tho?

local function PlayerRemoving(player)
	SaveData(player)
end

players.PlayerAdded:Connect(LoadData)
players.PlayerRemoving:Connect(PlayerRemoving)
game:BindToClose(function()
	for _, player in pairs(players:GetPlayers()) do
		coroutine.wrap(SaveData)(player)
	end 
end)

Have you tried debugging? And are you testing in studio or in-game?

I am testing in Studio. Could you explain how debugging will help please?