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 to datastore when purchasing object?

I am trying to make a datastore that adds an object to the players backpack and then saves when the player exits ...I am getting an error on this line 'Backpack is not a valid member of Player' which is line that is marked with <--- arrow below

This entire thing may be wrong I think I read you cannot save an object but I could try making a boolean ...I am new at scripting not sure if I am on the right track

local DSService = game:GetService('DataStoreService'):GetDataStore('ClownDataStore')

		function Purchase(tbl)
		
		local cost = tbl[1]
		local tool = tbl[2]
		local stats = tbl[3]
		
		stats.Value = stats.Value - cost 
		
		Objects[tool.Object.Value].Parent = script.Parent.PurchasedObjects 
		print (tool.Object.Value)--this is actually the string name of save it will print when you buy it (e.g., Mine, Dropper1, Dropper2...
		
		local plr = game.Players:GetPlayers()
		local key =  'tools_' .. tostring(plr.UserId)
    	local data  = DSService:GetAsync(key)

		if data then
		
		for i,v in pairs (game.Players:GetPlayers()) do
		
		script.Parent.PurchasedObjects[tool.Object.Value]:Clone().Parent = v.Backpack --me  after finding obejct put in player backpack
		end 
		
		else
        -- no data found
   	 	end	
		

		
		if Settings['ButtonsFadeOut'] then
			tool.Head.CanCollide = false
			coroutine.resume(coroutine.create(function()
				for i=1,20 do
					wait(Settings['FadeOutTime']/20)
					tool.Head.Transparency = tool.Head.Transparency + 0.05
				end
			end))
		else
			tool.Head.CanCollide = false
			tool.Head.Transparency = 1
		end
		end	
		
	

	script.Parent:WaitForChild('BuyObject').ChildAdded:connect(function(child)
		local tab = {}
		tab[1] = child.Cost.Value
		tab[2] = child.Button.Value
		tab[3] = child.Stats.Value
		Purchase(tab)
		
		wait(10)
		child:Destroy()
		end)
	

game.Players.PlayerRemoving:connect(function(plr)
	local key = 'tools_' .. tostring(plr.UserId)
    -- build a list of tools
    local saveList = {}
	
	plr:WaitForChild("Backpack")

    for _, tool in pairs(plr.BackPack:GetChildren()) do<--ERROR IS HERE
        table.insert(saveList, tool.Name)
    end

    DSService:SetAsync(key,saveList)

end)			
		
	
	function Create(tab)
		local x = Instance.new('Model')
		Instance.new('NumberValue',x).Value = tab[1]
		x.Value.Name = "Cost"
		Instance.new('ObjectValue',x).Value = tab[2]
		x.Value.Name = "Button"
		local Obj = Instance.new('ObjectValue',x)
		Obj.Name = "Stats"
		Obj.Value = tab[3]
		x.Parent = script.Parent.BuyObject
			
	end
	

	
	

2 comments

0

Looks like I'm having a similar issue with using DataStore, except I am using IntValues stored in the player

0

I fixed the error by adding this line "repeat wait() until plr.Backpack" but the datastore still does not save

1 answer

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