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

How do I make a DataStore for a folder?

Asked by 6 years ago

Currently I have been trying to figure out how I would go about making a DataStore for a folder in StarterPack, it basically already has a script that detects when you press a button, it will replace the script thats currently in your backpack and put the old one in a folder called OwnedAnimations, which is the one in StarterPack. So how would I go about saving whatever is in the OwnedAnimations folder so that when they load it loads their data in OwnedAnimations and when they leave it saves. I'm not very good at DataStores and they confuse me a lot, though I know all the basics of lua so try not to make it too hard to understand. Thanks.

1 answer

Log in to vote
0
Answered by 6 years ago

You can only save numbers, strings, Booleans, and tables to a DataStore(I'm probably missing something else). You also can't access DataStores from a localscript so you're gonna have to do all that from the server.

--ServerScriptService->Script
local ds = game:GetService("DataStoreService")

game.Players.PlayerAdded:connect(function(ply)
    local pdata = ds:GetDataStore("PData","p"..ply.UserId)   --Returns a DataStore using the p+ the player's UserId as a key/scope

    local animStorage = Instance.new("Folder",ply)
    animStorage.Name = "OwnedAnimations"

    local ownedAnims = pdata:GetAsync("OwnedAnims") --Returns a table saved to this key
    if ownedAnims then
        for i,v in pairs(ownedAnims) do --Cycles through ownedAnims table and stores data in a new Animation Instance's AnimationId
            local anim = Instance.new("Animation",animStorage)
            anim.AnimationId = v
        end
    end
end)

game.Players.PlayerRemoving:connect(function(ply)
    local pdata = ds:GetDataStore("PData","p"..ply.UserId)  --Same as before
    local animStorage = ply:FindFirstChild("OwnedAnimations"):GetChildren()

    local saved = {}
    for i,v in pairs do 
        table.insert(saved,v.AnimationId)
    end
    pdata:SetAsync("OwnedAnims",saved)
end)

I'd also suggest reading this if you still don't understand: http://wiki.roblox.com/index.php/Data_store

Ad

Answer this question