Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to save purchased items from a shop with datastore?

So currently I have a working shop where players can purchase stuff, and when they do a remote is fired to the server for which sword that was, then if the player has the money it will clone the tool from the serverstorage into the player's backpack. What I wanted to do is make it so these tools are saved and when a player joins the game automatically gives them their purchased tools.

My original idea was to have a bool value inside each tool in the serverstorage to check whether it had been purchased or not, but then I realised if I do that then when the value changes, the tool will show as bought for everyone? Sorry if that's confusing, just advice on how to begin creating this sort of system?

2 answers

Try this:

local ItemStore = game:GetService("DataStoreService"):GetDataStore("Items")
local Items = {plr.Backpack:FindFirstChild("")} --Add the name of the item and if you want to add more just do: , plr.Backpack:FindFirstChild("") and add the name of the other item

game.Players.PlayerAdded:Connect(function(plr)
	local vSave = ItemStore:GetAsync(PlayerKey)
	
	if vSave then
		Items = vSave[1]
	else
		ItemStore:SetAsync(PlayerKey, {Items})
	end 
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local PlayerKey = "player-"..plr.UserId
	
	ItemStore:SetAsync(PlayerKey, {Items})
end)

Put this in a script, If it works please click accept answer.

--MajinBluee

1 comment

0

Wouldn't everyone have the same items then? There's also better ways to do this.

Check out this answer that I gave to another scripter. Hope it helps. Have a great day scripting!

This discussion is preserved as it was. New answers, comments, and votes are closed.