I am trying to save data from a folder in replicated storage, though my script doesn't seem to work as nothing gets saved... I am not sure what I did wrong here. The code will be posted here:
local DataStoreService = game:GetService('DataStoreService') local WeapStore = DataStoreService:GetDataStore('WeapStore') local repStore = game:GetService('ReplicatedStorage') local OwnedWeaps = repStore.WeapOwned game.Players.PlayerAdded:Connect(function(player) local myWeaps = OwnedWeaps:Clone() myWeaps.Parent = player local dataTable = myWeaps:GetChildren() for _,v in pairs(dataTable) do print(player.UserId..v.Name) local data local success, errormessage = pcall(function() data = WeapStore:GetAsync(player.UserId..v.Name) end) if success then v.Value = data else warn(errormessage) end end end) game.Players.PlayerRemoving:Connect(function(player) local weapValueFolder = player:FindFirstChild('WeapOwned') local dataTable = weapValueFolder:GetChildren() for _,v in pairs(dataTable) do print(player.UserId..v.Name) local success, errormessage = pcall(function() WeapStore:SetAsync(player.UserId..v.Name, v.Value) end) if success then print('Weapons successfully saved!') else warn(errormessage) end end end)
If you are testing and you are the only one in you server, the server shuts down when you leave and the code wont run. To prevent this, add another block of code that includes this
game:BindToClose(function() for _, player in pairs(game.Players:GetPlayers()) do local Id = "player_"..player.UserId local Data = player.leaderstats.Cash.Value local success, errormessage = pcall(function() DataStore1:SetAsync(Id, Data) print("DataSaved") end) if success then print("DataSaved") elseif errormessage then print("There was a error") warn(errormessage) end end end)
Put this under your other code