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

How do you save removed parts through DataStore?

Asked by 5 years ago
Edited by DeceptiveCaster 5 years ago

So, on Pet Simulator you can buy new areas and the door deletes and when you rejoin it saves how do you make it stay removed as long as its bought? Here is my DataStore..

Handler..

    local storage = require(script:WaitForChild("Storage"))

    for i,v in pairs(game.Players:GetPlayers()) do

        if storage:GetData(v.UserId) == nil then

            storage:LoadData(v.UserId)

        end

    end



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

        storage:LoadData(player.UserId)

    end)



    game.Players.PlayerRemoving:connect(function(player)

        storage:SaveData(player.UserId)

    end)



    game.Close:connect(function()

        storage:SaveAllData()

    end)

Settings as it's called..

    local module = {

    Key = '2|4hXzO/N)t#Z9V*A8>Yz+G8&o&6',

    Version = '1'

    }



    return module

Storage..

local storage = {}

local settings = require(script.Parent:WaitForChild("Settings"))



local DataStoreService = game:GetService("DataStoreService")

local Datastore = DataStoreService:GetDataStore(settings.Key..settings.Version)



local function createData(id)

    local player = nil

    for i,v in pairs(game.Players:GetPlayers()) do

        if v.UserId == id then

            player = v

        end

    end

    local tab = {

name = player.Name,

userId = player.UserId

}

    local success, save = pcall(function()

        Datastore:SetAsync(tab.userId,tab)

    end)

    if not success then

        warn("An error occurred with saving " ..id.. " data: "..save)

    else

        warn("Successfully created " ..id.. " data: "..game:GetService("HttpService"):JSONEncode(tab))

    end

    table.insert(storage,#storage+1,tab)

    return tab

end



local module = {}



function module:GetData(id)

    local data = nil

    for i,v in pairs(storage) do

        if v.userId == id then

            data = v

        end

    end

    return data

end



function module:UpdateData(tab)

    for i,v in pairs(storage) do

        if v.userId == tab.userId then

            table.remove(storage,i)

        end

    end

    table.insert(storage,#storage+1,tab)

    return tab

end



function module:SaveData(id)

    local data = nil

    for i,v in pairs(storage) do

        if v.userId == id then

            data = v

            table.remove(storage,i)

        end

    end

    local success, save = pcall(function()

        Datastore:UpdateAsync(data.userId, function(oldData)

        local newValue = data

        return newValue

    end)

end)

if not success then

    warn("An error occurred with saving " ..id.. " data: "..save)

else

    warn("Successfully saved " ..id.. " data: "..game:GetService("HttpService"):JSONEncode(data))

end

return data

end



function module:LoadData(id)

    local data = nil

    data = Datastore:GetAsync(id)

    if data == nil then

        data = createData(id)

    else

        table.insert(storage,#storage+1,data)

    end

    return data

end



function module:DeleteData(id)

    local data = nil

    data = Datastore:RemoveAsync(id)

    return data

end



function module:SaveAllData()

    for i,v in pairs(storage) do

        local success, save = pcall(function()

            Datastore:UpdateAsync(v.userId, function(oldData)

            local newValue = v

            return newValue

        end)

    end)

    if not success then

        warn("An error occurred with saving " ..v.userId.. " data: "..save)

    else

        warn("Successfully saved " ..v.userId.. " data: "..game:GetService("HttpService"):JSONEncode(v))

    end

end

return nil

end



return module
0
Sorry I messed up the question xD ChefDevRBLX 90 — 5y
0
I put the code into a code block. DeceptiveCaster 3761 — 5y

Answer this question