So i had writen some code for a daily reward system, But i didnt know what time to use, all 3 all Diffirent and deltatime doesnt give the delay for some reason, Idk what to do and im really confused on wth deltatime is doing…
local S_run = game:GetService("RunService")
local pasttime = 0
local gametime = 0
local TRunTime = 0
local TStarttime = os.time()
S_run.Stepped:Connect(function(DeltaTime)
print("-----Log-----")
print(DeltaTime)
gametime += DeltaTime
TRunTime = os.time()-TStarttime
print(TRunTime)
print(gametime)
print("subtracting <<<")
print(TRunTime - gametime)
end)
Trying this code gives this in the output:
Again i have no idea what time to use, please help.
DeltaTime is the amount of seconds have passed since the last frame change. If the value is something around .017 is 60 FPS. This is so that physics can be calculated correctly and not speed up or slow down depending on how much FPS you have.
os.time is the amount of seconds that have passed since the year 1970 january the 1st 00:00.
For a daily reward system I’d advice you to use os.time(), what you’d is smtn like this:
local DatastoreService = game:GetService("DatastoreService")
local RunService = game:GetService("RunService")
local rewardTime = 24 * 60 * 60 -- this is equal to 24 hours
local function GiveReward(player: Player)
-- Blah blah blah
end
local function Touched(player: Player)
local timePassed = ( os.time() - DatastoreService:GetAsync(player.UserId) )
if (timePassed < rewardTime) then return end
local timeNow = os.time()
DatastoreService:SetAsync(player.UserId, timeNow) -- put this in a pcall or smtn and use profileservic or updateasync
GiveReward(player)
end
Please don’t copy my code, I just made this quickly as more of an example showcase thing. For the real thing make an instancevalue in the player and just set the value to the timeNow of the player datastore they join, then use that instancevalue to check if time has passed.
Oh and sorry if the code is incorrect, I wrote this in the devforum so there might be some api mistakes.
Because Stepped actually has 2 arguments, time and deltaTime…
time here refers to how long RunService has run. deltaTime, which is what you’re actually looking for, is the time between frames.
Please read the documentation first before posting for help…
Besides, another approach could be way better - don’t add time constantly, COMPARE times instead.
Specifically, every frame:
you compare the current time, and the time of which the player would receive the next day’s reward (hint: defaultRewardTime + 86400).
if the current time is equal to or more than the next day’s reward time, then you know a day has passed and the player can receive the reward again.
Hell, if you’re concerned about efficiency (and are okay with giving up a bit of accessibility) you don’t even have to do it per frame - do it ONLY ONCE when the player first joins.
Yea thanks for the solution! But im trying to keep a buffer updated every 2 seconds
therefor i need a gametime variable to keep track of that, so thanks and ill try implementing it!