I finished making my commission person his tool shop and then he asked can you make a data storage. So I tried to make a data storage script that saves the tools inside the folder. So I wanted to combine that script and the data storage script together. I failed but I tried to do it. Here’s what I did
This is the data storage script:
local ToolFolder = game:GetService("ServerStorage"):FindFirstChild("SavedTools")
local dataStoreService = game:GetService("DataStoreService")
local SaveData = dataStoreService:GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(player)
local ToolData = SaveData:GetAsync(player.UserId)
local Backpack = player:WaitForChild("Backpack")
local StarterGear = player:WaitForChild("StarterGear")
if ToolData ~= nil then
for i, v in pairs(ToolData) do
if ToolFolder:FindFirstChild(v) and Backpack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
ToolFolder[v]:Clone().Parent = Backpack
ToolFolder[v]:Clone().Parent = StarterGear
end
end
end
player.CharacterRemoving:Connect(function(Character)
Character:WaitForChild("Humanoid"):UnequipTools()
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local ToolTable = {}
for i,v in pairs(player.Backpack:GetChildren()) do
table.insert(ToolTable, v.Name)
end
if ToolTable ~= nil then
SaveData:SetAsync(player.UserId,ToolTable)
end
end)
This is the script for the tool shop:
local ServerStorage = game:GetService("ServerStorage")
game.ReplicatedStorage.ToolEvents.OrangeEvent.OnServerEvent:Connect(function(player)
if player.leaderstats.Pet_Slices.Value >= 100 then
player.leaderstats.Pet_Slices.Value = player.leaderstats.Pet_Slices.Value - 100
game.ServerStorage.Tools.Orange:Clone().Parent = player.Backpack
game.ServerStorage.Tools.Orange:Clone().Parent = ServerStorage.SavedTools
end
end)
What I tried to make it in the last two lines so that the orange goes into the backpack and the SavedTools folder and had hopes that it would save the orange after leaving the game. But unfortunately it did not work
Firstly, have you made sure the orange is inserted inside this table here?
Secondly, the above for loop is dangerous as it doesn’t guranteed to save the player’s tools correctly. This is because this will happen if the player leaves the game whilst their character model is removed/they’re dead.
Thirdly, why are you making it so that only when the OrangeEvent gets fired, then you add the orange to the SavedTools folder? You should’ve add the orange to SavedTools instead of scripting it.
That was my first thought but then I realised if the player does not have 100 Pet_Slices then he cannot buy the tool. So I thought if the Pet_Slices is more than 100 then I thought of putting it into SavedTools folder thinking that it would save any tool that is inside of SavedToolsfolder
You would have to make a table that consists of whatever tool’s names the player owns in string form. Then, you can save it and when loading it, you refer whatever value inside that table and find the corresponding tool to load and give for the player.
Just handle it on the server so that when they buy the item save it to the datastore. Also sometimes in studio data wont be saved on time. This is because the server is hosted on your local machine and when you stop it from running the server dies immediately. You can delay the shutdown however using Game:BindToShut()
It will. If you use a datastore per player for their weapons and you save each item with a new key then you will not hit the limits. also adding a debounce is a thing.
Also superkingburger1456, get the weapon/s or values right when the player leaves and save them in a variables. This will make your chances of data loss go down by a-lot.
local DSS=Game:service'DataStoreService';
local ds=DSS:GetDataStore(Player.UserId..'_Tools');
ds:UpdateAsync('Orange',function()
return(true); -- true once they bought it.
end);
You can also use datastore2 to reduce data loss even further.