I want to make a system that saves some Values in ServerStorage but i dont know how to make it, if someone can please help me, here’s the script i made, i cant find the mistake in it and idk what to do to fix it so it saves that values too.
local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("datastore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local money = Instance.new("IntValue", leaderstats)
money.Name = "Coins"
money.Value = 0
money.Value = datastore:GetAsync(player.UserId.."Coins") or 0
end)
function savedata(player)
datastore:SetAsync(player.UserId.."Coins", player.leaderstats.Coins.Value)
datastore:SetAsync(player.UserId.."MediumTower", game.ServerStorage.SaveBarriers.Zone1Barrier.Value)
datastore:SetAsync(player.UserId.."HardTower", game.ServerStorage.SaveBarriers.Zone2Barrier.Value)
end
game.Players.PlayerRemoving:Connect(function(player)
savedata(player)
end)
game:BindToClose(function()
for i, player in ipairs(game.Players:GetPlayers()) do
savedata(player)
end
end)
If someone can help me to find the mistake i would appreciate it a lot, thanks.
Are you trying to save data that persists across sessions? or just in the server alone? If so you should definitely look into DataStoreService, because this method will not work.
Actually, I’m pretty confused by what you’re trying to do, because you’re already using datastoreservice? What is the game:BindToClose() for?
In the case the game suddenly shutdowns, for example a Roblox error or the developer shutting down the server, this event will fire and will save the data for all players.
You are only using the :SetAsync() functions which you are saving the data with. To get the data, use :GetAsync(). I see that your code isn’t protected from errors, you should do that as well.
local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("datastore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local money = Instance.new("IntValue", leaderstats)
money.Name = "Coins"
local Data
local success, errormessage = pcall(function()
Data = datastore:GetAsync(player.UserId)
end)
if success and Data then
money.Value = Data.Coins
game.ServerStorage.SaveBarriers.Zone1Barrier.Value = Data.Barrier1
game.ServerStorage.SaveBarriers.Zone2Barrier.Value = Data.Barrier2
end
end)
function savedata(player)
local TableOfDatas = {
Coins = player:WaitForChild("leaderstats").Coins.Value,
Barrier1 = game.ServerStorage.SaveBarriers.Zone1Barrier.Value,
Barrier2 = game.ServerStorage.SaveBarriers.Zone2Barrier.Value
}
local success, errorMessage = pcall(function()
datastore:SetAsync(player.UserId, TableOfDatas)
end)
if success then
warn(player.Name.."'s data was successfully saved!")
else
warn("Error when saving "..player.Name.."'s data!!!")
warn(errorMessage)
end
end
game.Players.PlayerRemoving:Connect(function(player)
savedata(player)
end)
The :BindToClose() function is not needed because the .PlayerRemoving function runs everytime a player leaves the server, NO MATTER WHAT. I’ve made the code shorter, I hope you will understand what I changed.
Sorry, the indentation is incorrect. For some reason I try to edit it and indentation does not change.
And also, I have no idea why you are trying to put put the user ids and other values together… Makes no sense…