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)

1 comment
you also need to add a game:BindToClose() function to make sure to save everyone's data before the server shuts down