Help needed with Tool shop data storage

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.

Thanks for the help

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

Thanks for the help again

1 Like

Actually so is there any script that only saves the tool when the player has bought it?

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.

Could you please give the script. I can pay 100 Robux if you want me to. I’m still a beginner so It’s kind of complicated to me

Giving free scripts won’t help you to improve your scripting skills. Don’t try to bribe me with robux. Learn it by yourself and follow what I’ve said.

1 Like

I’m really sorry for what I did. I’m really really sorry. What you said is actually the best peice of advice

Save the names of the tools, then put them in a table and save. After, get the table and get the indexes of the names so you can clone the tools.

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()

Player leaving is better than just saving it when they buy, because they can spam buy weapons and then some of their data won’t save.

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.

1 Like

The debounce would have to be very long.

Just use playerleaving events to save.

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.

1 Like

Using updateasync is also a better approach than setasync.

1 Like

Not really since 1 second is enough time.

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.

1 Like