You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve? Keep it simple and clear!
Save and load datastores while in game and studio
- 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:
- 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)