DataStore saving and loading while in Studio, only loads and doesn't save while in game

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

Save and load datastores while in game and studio

  1. What is the issue? Include screenshots / videos if possible!

Datastores are not saving while in game, but are while in studio

In Studio:

In Game:

  1. What solutions have you tried so far? Did you look for solutions on the Creator Hub?

I’ve tried multiple different BindToClose functions, alterations to my saving function, restarting studio, and even copying other similar DataStores, but to no avail.

-- Services
local DataStoreService = game:GetService("DataStoreService")
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local DataStore = DataStoreService:GetDataStore("playerData")
local UpdateEquipment = ReplicatedStorage.EventStorage.UpdateGearCharacterDeath


local function deepClone(original)
	local clone = table.clone(original) 
	for key, value in original do
		if type(value) == "table" then
			clone[key] = deepClone(value)
		end
	end
	return clone
end


local data = {
	Coins = 0;
	Equipment = {};
	EquippedGear = {};
}

function GearSync(newParent, GearTable)
	
		for i,v in pairs(GearTable) do
			for a,b in pairs(ReplicatedStorage.UserGear:GetChildren()) do
				if v == b.Name then

					local newEquipment = b:Clone()
					newEquipment.Parent = newParent

				end
			end

		end

end

function GetNames(original)
	
	local clone = {}
	
	for i,v in pairs(original) do
		table.insert(clone,v.Name)
	end
	
	
	return clone
end


local function load(player:Player)
	
	
	local playerID = player.UserId
	

	if not DataStore:GetAsync(playerID) then
		DataStore:SetAsync(playerID, deepClone(data))
	end
	

	
	local playerCoins = player:WaitForChild("leaderstats").Coins
	local playerEquipment =  ServerStorage:WaitForChild(player.Name.."_Equipment")
	local playerEquippedEquipment = ServerStorage:WaitForChild(player.Name.."_EquippedEquipment")

	
	local success, errorMessage = pcall(function()
		data = DataStore:GetAsync(playerID)
	end)
	if success then
		playerCoins.Value = data.Coins
		GearSync(playerEquipment, data.Equipment)
		GearSync(playerEquippedEquipment, data.EquippedGear)
		
		for i,v in pairs(playerEquippedEquipment:GetChildren()) do
			
			UpdateEquipment:FireClient(player, v:GetAttribute("ItemName"))
			
		end
	end
	
end


local function save(player:Player)
	
	local playerID = player.UserId
	
	local dataToSave = {
		
		Coins = player.leaderstats.Coins.Value;
		Equipment = GetNames(ServerStorage:WaitForChild(player.Name.."_Equipment"):GetChildren());
		EquippedGear = GetNames(ServerStorage:WaitForChild(player.Name.."_EquippedEquipment"):GetChildren());
		
		
	}
	
	local success, errorMessage = pcall(function()
		DataStore:SetAsync(playerID, dataToSave)
	end)
	if success then
		print("DataStored")
	else
		warn(errorMessage)
	end
	
	
end


function UpdatePeriodically(player)
	
	while true do
		task.wait(60)
		
		save(player)
		
	end
	
end



game:BindToClose(function()

	if RunService:IsStudio() then task.wait(3) return end 
	
	for _, player in ipairs(Players:GetPlayers()) do
		save(player)
	end
	task.wait(5)
end)


Players.PlayerAdded:Connect(load)
Players.PlayerAdded:Connect(UpdatePeriodically)
Players.PlayerRemoving:Connect(save)

Update: It seems to work now, I haven’t changed anything in the script itself but it just decided to work. Gonna leave this for anyone who might have the same issue.

Edit: After reviewing it a bit more, I think I might’ve figured it out. In another script I was deleting the player Equipment folder and EquippedEquipment folder on the playerRemoved event. So, removing that part from that script and into the DataStore script after the data is saves should fix it I hope.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.