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

Configure a data store to change what it saves?

Asked by 9 years ago

This is a datastore (That works 100%) is there a way for the datastore to insert a model into the player like it does with leaderstats, and save the weapons that are inputted into starter gear while in game? There are 2 scripts to this ill put them both down. Value in main script is a bool value.

MainScript:

local DataStore = game:GetService("DataStoreService"):GetDataStore("MoneyStore")


game.Players.PlayerAdded:connect(function(player)
     local leaderstats = Instance.new("Model", player)
     leaderstats.Name = "leaderstats"

    local money = Instance.new("IntValue", leaderstats)
     money.Name = "Points"
     money.Value = 50
    if script.UseDataStore.Value == true then
        local key = "user_" ..player.userId
        local savedValues = DataStore:GetAsync(key)
        local valuesToSave = {money.Value}

        if savedValues then
            -- Save format: {money}
            money.Value = savedValues[1]
        else
            DataStore:SetAsync(key, valuesToSave)

        end
        game.Players.PlayerRemoving:connect(function(player)
            local money = player.leaderstats.Points
            local valuesToSave = {money.Value}
            local key = "user_" ..player.userId 
            DataStore:SetAsync(key, valuesToSave)
        end)
    end
end)

Script inside main script

if script.Parent.UseDataStore.Value == true then
    function pAdded(player)
        local leaderstats = player:FindFirstChild("leaderstats")
        while true do
            if leaderstats ~= nil and player.Character ~= nil then
                break
            end
            wait(1)
        end
        local msg = Instance.new("Message",player.Character)
        msg.Text = "Loading Data..."
        player:WaitForDataReady()
        local savedPoints
        local savedPoints2
        local successFunc = pcall(function() savedPoints = player:LoadNumber("money") end)
        if not successFunc or savedPoints == nil or savedPoints2 == nil then
            msg.Text = "Failed to load."    
            wait(5)
            msg:remove()
        else
            if player.leaderstats:FindFirstChild("Points") ~= nil then
                player.leaderstats.Points.Value = savedPoints
            end
            msg.Text = "Loaded"
            wait(5)
            msg:remove()
        end
    end
    game.Players.PlayerAdded:connect(pAdded)

    function pRemove(player)
        if player:FindFirstChild("leaderstats") ~= nil then
            if player.leaderstats:FindFirstChild("Points") ~= nil then
                if not pcall(function() player:SaveNumber("Points", player.leaderstats.Points.Value) end) then end
            end
        end
    end

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

Value in main script is a bool value.

Answer this question