Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to save to datastore when purchasing object?

Asked by
Glistre -7
7 years ago

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




0
Looks like I'm having a similar issue with using DataStore, except I am using IntValues stored in the player T0XN 276 — 7y
0
I fixed the error by adding this line "repeat wait() until plr.Backpack" but the datastore still does not save Glistre -7 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

its not BackPack its Backpack

Ad

Answer this question