I’m still noob at scripting so I wasn’t able to create a time counter that saves myself
I wasn’t able to find anywhere something like that I was trying to make
local second = 0
local minute = 0
local hour = 0
while wait(1) do
if second >= 60 then
second -= 60
minute += 1
end
if minute >= 60 then
minute -= 60
hour += 1
end
second += 1
script.Parent.Text = "second: ".. second.." minute: "..minute.." hour: "..hour
end
When player is in the game then it counts time and when leaves then it dosent do anything until player rejoins the game then it will continue where it stopped
Alright, you will need to use datastores. There are plenty of tutorials on that so I’m sure you’re fine in that area.
For the time I would recommend making it just one number: the seconds, and converting all the seconds into minutes, hours, etc. But you can also use all three numbers.
You would use a RemoteEvent (or BindableEvent depending on what type of script this is) and have the data saving script send the timer script the time left, and have it count down from there.
depending on your statement here is the timer script feel free to get anything from the script i dont mind
added some comments for you to understand better as you are a beginner
-- Import required services and modules
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TimerDataStore = DataStoreService:GetDataStore("PlayerTimerData")
-- Set the timer interval in seconds (how frequently the timer updates)
local TimerInterval = 1
-- Variable to keep track of the timer job
local TimerJob
-- Function to save the player's timer data to the DataStore
local function SaveTimerData(player, time)
local key = player.UserId
local success, error = pcall(function()
TimerDataStore:SetAsync(tostring(key), time)
end)
if not success then
warn("Failed to save timer data for player " .. player.Name .. ": " .. error)
end
end
-- Function to start the timer for a player
local function StartTimer(player)
-- Create a new connection to the Heartbeat event
TimerJob = game:GetService("RunService").Heartbeat:Connect(function(dt)
local character = player.Character
if character then
-- Increment the time and display it in the client
local time = (TimerDataStore:GetAsync(tostring(player.UserId)) or 0) + dt
SaveTimerData(player, time)
end
end)
end
-- Function to stop the timer for a player
local function StopTimer(player)
if TimerJob then
TimerJob:Disconnect()
TimerJob = nil
end
end
-- Handle player join and leave events
game.Players.PlayerAdded:Connect(function(player)
local time = TimerDataStore:GetAsync(tostring(player.UserId)) or 0
StartTimer(player)
end)
game.Players.PlayerRemoving:Connect(function(player)
StopTimer(player)
end)
no it should do all of the work for you. You just need to know how to use datastores to check times or just use the variable for getting datatable at playeradded and add the code there
Do I add the script to the textlabel that I’m using or into serverscriptservice? and the script has to be script on localscript right? lol so many questions sorry