#Leaderstats Autosaving - How to make it?
73 messages · Page 1 of 1 (latest)
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
** You are now Level 1! **
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
did you get this from an ai or something
thats a bad habit
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
he wanted autosave
he already had playerleaving
bro
autosave just means that you don't have to save it manually
jesus christ
gimme a second
that's what was implied by his script bro its quite obvious come on man
he had playerleaving
and was asking for autosave
its implied bro use common sense
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
i agree with you that's its a waste of resources
** You are now Level 5! **
why not let it just save when the player leaves the game, something that every single game does
alright that's good
because he already had it and he was asking for autosave
some games still use autosave, for reasons i dont know
but that IS autosave
** You are now Level 12! **
older roblox games use an autosave every set amount of minutes, newer roblox games use an autosave when the player leaves
it provides more reliability in games that need it a lot
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
man i remember that game
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
its up to him, but he already had what you say "autosaving" is
im not sure what else there is
i don't think so
he should use a pcall
gimme a second
lemme open up roblox studio
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