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

[SOLVED] What is wrong with this Data Store Script? [closed]

Asked by 10 years ago

I am trying to create a script that will save anything in a player's backpack when they leave and/or die, and load it back into their backpack when they return. Am I missing something here?

local DataStoreService = game:GetService("DataStoreService")
game.Players.PlayerAdded:connect(
function(Player)
    local PlayerStore = DataStoreService:GetDataStore(tostring(Player.userId), "PlayerData")
    local PlayerItems = PlayerStore:GetAsync("PlayerItems")
    if not PlayerItems then
        PlayerItems = Player.Backpack:GetChildren()
        PlayerStore:SetAsync("PlayerItems", PlayerItems)
    end

P.S. I am aware there isn't any code for when they die, because I don't know what I need to do for that.

1
There should be a ) after the last end. trogyssy 221 — 10y
1
X| wow I missed that. thx! :) AwsomeSpongebob 350 — 10y
0
no problem :P trogyssy 221 — 10y
1
But you need to find a different way to store the values. The items cannot be stored. The table needs to be strings, bools, or numbers trogyssy 221 — 10y
0
Please post an answer to your question if you solved it. User#11893 186 — 10y

Locked by TheMyrco

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
1
Answered by
DrJonJ 110
10 years ago

I made this exact thing. Here's my code edited for your needs:

local key = 'weps'
local exclude = {
    'StarterSword';
}
-- I suggest placing the name of the initial sword here to
-- prevent dupes. Also place VIP tool(s) (if any) names here

game.Players.PlayerAdded:connect(
    function(p)

        local weps = data:GetAsync(p.userId..'_'..key)

        if weps then -- if it has already been saved
            for _,v in pairs(weps) do
                if game.ReplicatedStorage:FindFirstChild(v) then
                    game.ReplicatedStorage[v]:clone().Parent = p.StarterGear
                end
            end
        end

        p.StarterGear.ChildAdded:connect(
            function(c)
                local tools = { }
                for _,v in pairs(p.StarterGear:GetChildren()) do
                    local add = true
                    for _,c in pairs(excluded) do
                        if v.Name ~= c.Name then
                            add = false
                        end
                    end
                    if add then
                        table.insert(mo, v.Name)
                    end
                end
                data:SetAsync(p.userId..'_'..key),  tools) 
            end
        )
    end
)

The result is a table with the names of the tools and/or hopperbins.

Note: This script saves an the tools whenever an item is added to StarterGear. When giving a player a tool you want them to keep, I suggest placing it in their Backpack as well as their StarterGear. Doing so makes it so you don't need to keep re-adding the tools upon dying/respawning.

Ad