#Leaderstats Autosaving - How to make it?

73 messages · Page 1 of 1 (latest)

ebon citrus
#

datastores with onplayerleave event

#

oh wait nvm

#

i didnt read the script i just read the title#

#

use runservice.heartbeat
add the time from heartbeat to a variable
check if the variable from the stage above is over the autosave amount
if it is over the autosave amount make the variable 0 again and autosave

rugged thistleBOT
#

studio** You are now Level 1! **studio

ebon citrus
#

no problem, if you need help implementing this let me know

#

i dont know if i explained it well

#

what part do u not understand i can walk you through

#

so make a varibale called autosaveamount
make the variable the time you want to wait, for example, if you wanted the autosave to happen every 60 seconds then you would do
local AutosaveTimer = 60
then make another variable such as timesinceautosave and make it 0
local TimeSinceAutosave = 0
then add the time from the heartbeat to the timesinceautosave
TimeSinceAutosave = TimeSinceAutoSave + Time (the time comes from the runservice.heartbeat)
then check to see if TimeSinceAutosave is greater than AutoSaveAmount
if TimeSinceAutosave > AutoSaveAmount then
-- save data

brittle birch
#

did you get this from an ai or something

brittle birch
#

if you want autosave just save when the player leaves

#

does the code even work when you're using :connect() instead of :Connect()

#

you should clean it up

#

💀

#

i'm surprised

#

anyway

#

i'm not trying to insult you but trying to help you learn

#

but the script is very messy

#

why are you calling the variables points1 and points2

#

you're repeating a lot of very similar names

ebon citrus
#

he already had playerleaving

brittle birch
#

bro

#

autosave just means that you don't have to save it manually

#

jesus christ

#

gimme a second

ebon citrus
#

he had playerleaving

#

and was asking for autosave

#

its implied bro use common sense

brittle birch
#

no bro

#

are we talking about the same thing

#

you coded it so it saves every X minutes

#

but that's a waste of resources

ebon citrus
#

i agree with you that's its a waste of resources

rugged thistleBOT
#

studio** You are now Level 5! **studio

brittle birch
#

why not let it just save when the player leaves the game, something that every single game does

brittle birch
ebon citrus
#

some games still use autosave, for reasons i dont know

brittle birch
rugged thistleBOT
#

studio** You are now Level 12! **studio

ebon citrus
#

yes it is but i mean in here

#

how its implied

brittle birch
ebon citrus
#

such as miners haven

#

where people are online for a long time

#

and outages can happen

#

since they dont want to loose about 20 hours of progress

#

its widely used but not shown

#

playerremoving is common practice though

brittle birch
#

anyway

#

i mean no disrespect to the guy that posted this

#

but that's probably not what he wants

#

his code is very messy so i don't assume that he's working with a very large scale game

#

you're teaching him bad habits this way

ebon citrus
#

im not sure what else there is

brittle birch
#

i don't think so

#

he should use a pcall

#

gimme a second

#

lemme open up roblox studio

brittle birch
#

yo

#
--// Variables

local DataStore = game:GetService("DataStoreService")
local PointsDS = DataStore:GetDataStore("PointsDataStore") 

local startAmount = 0 

local playersLeft = 0 

--// Functions

game.Players.PlayerAdded:connect(function(player)
    -- Player added

    playersLeft += 1
    
    -- leaderstats
    
    local leaderstats = Instance.new("Folder") -- leaderstats folder
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    
    -- Points: leaderstats
    local points = Instance.new("IntValue") -- Points value
    points.Name = "Points"
    points.Value = 0
    points.Parent = leaderstats

    -- Points: DataStore

    local points_data 

    pcall(function()
        points_data = PointsDS:GetAsync(player.UserId.."-Points")
    end) 

    if points_data ~= nil then 
        -- Player has saved data, load it in.
        points.Value = points_data
    else 
        -- New player, give fixed amount of starting cash.
        points.Value = startAmount
    end
end)

local bindableEvent = Instance.new("BindableEvent") -- BindableEvent that triggers when the game closes

game.Players.PlayerRemoving:connect(function(player)
    -- Player left
    pcall(function()
        PointsDS:SetAsync(player.UserId.."-Points", player.leaderstats.Points.Value) -- Sets the value of the player's PointsDataStore to his in-game Points Value
    end)

    print("Data saved")

    playersLeft -= 1
    bindableEvent:Fire()

end)

game:BindToClose(function()
    -- This will be triggered upon shutdown.
    while playersLeft > 0 do 
        bindableEvent.Event:Wait() -- Game will only close once ALL of the players' data is saved
    end
end)
#

man datastores are so ass

#

anyway

#

here is an improved script with an explanation for every part of it

#

there's probably a more efficient way to do it

#

or a safer way with local suc, err = pcall(function())

#

but this works