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

Save IntValues on Player?

Asked by 3 years ago

So far I have been able to save data on leaderstats. Next what I want to do is save the value of how much it costs to rebirth. I cant seem to find anything online about it except how to save the leaderstats which I already did.

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

local function onPlayerJoin(player)

    local leaderstats = Instance.new("Folder") -- Don't Change
    leaderstats.Name = "leaderstats" -- Don't Change
    leaderstats.Parent = player -- Don't Change

    local Potatoes = Instance.new("IntValue")
    Potatoes.Name = "Potatoes"
    Potatoes.Parent = leaderstats

    local Rebirths = Instance.new("IntValue")
    Rebirths.Name = "Rebirths"
    Rebirths.Parent = leaderstats

    local GoldenPotatoes = Instance.new("IntValue")
    GoldenPotatoes.Name = "GoldenPotatoes"
    GoldenPotatoes.Parent = leaderstats

    local Music = Instance.new("BoolValue")
    Music.Name = "Music"
    Music.Parent = player
    Music = true

    local OneRebirthInt = Instance.new("IntValue")
    OneRebirthInt.Name = "OneRebirthInt"
    OneRebirthInt.Parent = player


    local playerUserId = 'Player_' .. player.UserId 
    local data = ds:GetAsync(playerUserId)
    if data then
        Potatoes.Value = data['Potatoes']
        Rebirths.Value = data['Rebirths']
    else
        Potatoes.Value = 0
        Rebirths.Value = 0
    end

end

local function create_table(player)
    local player_stats = {}
    for _, stat in pairs(player.leaderstats:GetChildren()) do
        player_stats[stat.Name] = stat.Value
    end
    return player_stats
end

local function onPlayerExit(player)
    local player_stats = create_table(player)
    local success, err = pcall(function()
        local playerUserId = 'Player_' .. player.UserId
        ds:SetAsync(playerUserId, player_stats)

    end)

    if not success then
        warn('Could not save data')
    end

end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
0
Do you want to make it save the value of the player's next rebirth? horacioMiquen 15 — 3y
0
No i want to save the values of how much it costs to rebirth General_ChaosTv 2 — 3y

Answer this question