okay so I’m scripting this data store and I’m really confused because I’ve never experienced this type of error before
function SaveData(Donated, Raised, Robux, Player)
print("Saving data.")
local Data = {}
Data.Donated = Donated
Data.Raised = Raised
Data.Robux = Robux
-- does anything before here
MainDataStore:SetAsync(Player.UserId, Data) -- fails here with no error
print("Saved data.")
end
so it does anything before setasync but fails on the same line. I am so confused and any help would be awesome, thanks.
game:BindToClose(function()
for i,player in pairs(game.Players:GetChildren())do
SaveData(donated,raised,robux,player)
end
end)
bindtoclose won’t fail to work in studio, this is how I fixed it when I faced the same issues. (This is additional to when server crashes, or game shutdowns, you must always have the PlayerRemoving event too, cause only then it will save players data when they leave game from a multi player server)
You wouldn’t do that. Because if a player leaves the game then his data would not be saved.
You have to save data when player leaves.
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreName")
Players.PlayerRemoving:Connect(function(Player)
local Leaderstats = Player:FindFirstChild("leaderstats")
local CashValue = Leaderstats:FindFirstChild("Cash")
if RunService:IsStudio() then return end --This line will prevent the studio from continuing
local Success, Response = pcall(function()
DataStore:SetAsync(Player.UserId, CashValue.Value)
end)
if not Success then
warn("Error occured during DataStore saving: " .. Response)
end
end)
Also don’t forget that this is a server-side script.