How do I make a timer save? Lets say I set a 1 minute timer. When the player leaves, it will reset. So how do I make it save? that means if the timer says 50 seconds and the player leaves, when the player returns it will still be at 50 seconds.
Considering its against the rules to give out free scripts on this section of the forum, I’ll just tell you how rather then giving you the complete script…
First thing, Im assuming you already have the timer made. Lets just go over the basic saving and loading
The easiest way to do this is ofc using DataStoreService to just save the single value, I’ve included a bit of code below on saving and loading the value.
--TopOfScript
local DataStoreService = game:GetService("DataStoreService")
local Datastore = DataStoreService:GetDataStore("TimerSave")
-- Saving
local TimerValue = --Path to timer value
local Key = Player.UserId
Datastore:SetAsync(Key, TimerValue)
-- Loading
local NewTimerVal
local Key = Player.UserId
local good, info = pcall(function()
return Datastore:GetAsync(Key)
end)
if good and info then
NewTimerVal = info
end
print(NewTimerVal)
This code will have to adapted to your application, but if you have basic scripting knowledge, it won’t be hard to put around PlayerAdded and PlayerRemoving events to get the value to save.
Any questions?
Yup. I do have my timer set up already. May I know which is the part where the timer saves?
Yep,
-- Saving
local TimerValue = --Path to timer value
local Key = Player.UserId
Datastore:SetAsync(Key, TimerValue)
Then the loading is right beneath it.
So by Path I just have to say like game.Serverstorage…
Like where ever the value is, like it can be a variable inside the script, just wherever that Number that the timer shows is.
Wait… I dont quite understand you
Alright say you have a basic timer set up.
for i=30,1,-1 do
print(i)
end
i is the timer value, its going to count from 30 to 1…Say the timer says you have 53 seconds left, you want to save 53
So I just put 53? inside the path?
Only if the timer is at that…
You want to save whatever number the timer says… So say it has 23 seconds, you save 23. If it has 51, save 51, etc
This is my timer script
local timer = script.Parent
local minutes = 1
local seconds = 0
repeat
if seconds <= 0 then
minutes = minutes - 1
seconds = 59
else
seconds = seconds - 1
end
if seconds <= 9 then
timer.Text = tostring(minutes)..":0"..tostring(seconds)
else
timer.Text = tostring(minutes)..":"..tostring(seconds)
end
wait(1)
until minutes <= 0 and seconds <= 0
script.Parent.Parent.Enabled = false
Alright, so you want to save the Seconds and Minutes Value…
I’d personally just do
local TimerValue = (minutes*60)+seconds
to get the total number of seconds, then just save that number
Then when you go to load it, do
local minutes = math.floor(NewTimerVal/60)
local seconds = NewTimerVal - (minutes*60)
So which part do I do in this script and in the datastore script?
--TopOfScript
local DataStoreService = game:GetService("DataStoreService")
local Datastore = DataStoreService:GetDataStore("TimerSave")
local timer = script.Parent
local minutes = 1
local seconds = 0
function Save()
local TimerValue = (minutes*60)+seconds
local Key = Player.UserId
Datastore:SetAsync(Key, TimerValue)
end
function Load()
local NewTimerVal
local Key = Player.UserId
local good, info = pcall(function()
return Datastore:GetAsync(Key)
end)
if good and info then
NewTimerVal = info
end
minutes = math.floor(NewTimerVal/60)
seconds = NewTimerVal - (minutes*60)
end
repeat
if seconds <= 0 then
minutes = minutes - 1
seconds = 59
else
seconds = seconds - 1
end
if seconds <= 9 then
timer.Text = tostring(minutes)..":0"..tostring(seconds)
else
timer.Text = tostring(minutes)..":"..tostring(seconds)
end
wait(1)
until minutes <= 0 and seconds <= 0
script.Parent.Parent.Enabled = false
You will still need define player and decide when to save and load data, but I think is about as much as I can give you without it completely giving you the script.
This will work. Thank you so much!
I will get back to you though if I run into anything