Hey guys! I am making a simulator game right now and I am stuck on something. I want to have it so you can buy a better tool that when you click it will give you more coins than the tool before. But the problem is that I don’t know how to make it so that the new tool will save when you leave the game. I have no progress on it, all that I have is the tools. Please help, thank you! I have also made the leaderstats.
this script should work for saving tools or items:
local dataservice = game:GetService("DataStoreService")
local inventorydata = dataservice:GetDataStore("Inventory")
local itemsfolder = game.ServerStorage.ItemsFolder --replace it to the location of your item folder
game.Players.PlayerAdded:Connect(function(player)
local inventory = inventorydata:GetAsync(player.UserId) or {}
for i, si in pairs(inventory) do
if itemsfolder:FindFirstChild(si) then
itemsfolder.si:Clone().Parent = player.Inventory
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local inventory = {}
for i, si in pairs(player.Inventory:GetChildren()) do
table.insert(si)
end
local success, errormsg = pcall(function()
inventorydata:SetAsync(player.UserId, inventory)
end)
if errormsg then warn(errormsg)
end)
Thank you so much I will try it!
It did not work. I put it in server script service and changed “ItemsFolder” with the name of the folder and it did not work. @sniper74will
you missed some other important parts. inside of the part where it would save it gets the children of a folder inside of the player you also have to change those names.
here is the same script with side notes of what you need to change:
local dataservice = game:GetService("DataStoreService")
local inventorydata = dataservice:GetDataStore("Inventory")
local itemsfolder = game.ServerStorage.ItemsFolder --replace it to the location of your item folder
game.Players.PlayerAdded:Connect(function(player)
local inventory = inventorydata:GetAsync(player.UserId) or {}
for i, si in pairs(inventory) do
if itemsfolder:FindFirstChild(si) then
itemsfolder[si]:Clone().Parent = player.Inventory--Change this to the folder you want to copy the items back into.
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local inventory = {}
for i, si in pairs(player.Inventory:GetChildren()) do-- Change this to the folder you want to save the children of.
table.insert(si.Name)
end
local success, errormsg = pcall(function()
inventorydata:SetAsync(player.UserId, inventory)
end)
if errormsg then warn(errormsg)
end)
Ensure that you also use game:BindToClose
event the same as PlayerRemoving
, as sometimes it may close the server before PlayerRemoving
would run and the data will be lost. For more details on how to use it, https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose. It’s recommended to use it in every datastore to prevent loss.
Ok I will try this stuff out! Thank y’all for helping!
what if we used replicatedStorage to save the weopons
its the exact same, but I’d recommend serverstorage as exploiters cant view it.
sniper could u make a short video of how to do it for server storage
no, thats not what I meant. You don’t NEED to it’s just recommended. Also, I was talking about your item folder.