Question regarding Tick()

I have the following code responsible for handling daily logins:

local dataStoreService = game:GetService("DataStoreService")
local TableDataStore = dataStoreService:GetDataStore("TagsTable")

local Awards = {
	Day1 = 5000,
	Day2 = 10000,
	Day3 = 15000,
	Day4 = 25000,
	Day5 = "Skilled Tag"
}

local Hours24 = 86400
local Hours48 = 172800

local NotClaimedColor = Color3.new(8 / 255, 255 / 255, 226 / 255)
local ClaimedColor = Color3.new(90 / 255, 90 / 255, 255 / 255)   


game.Players.PlayerAdded:Connect(function(player)
	local UserIdKey = player.UserId .. "LoginData"

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "Login"
	leaderstats.Parent = player

	local DailyLogin = Instance.new("NumberValue")
	DailyLogin.Name = "DailyLogin"
	DailyLogin.Parent = leaderstats

	local LoginTime = Instance.new("NumberValue")
	LoginTime.Name = "LoginTime"
	LoginTime.Parent = leaderstats


	local data

	local Success, errormessage = pcall(function()
		data = TableDataStore:GetAsync(UserIdKey)
	end)

	if data then
		

		DailyLogin.Value = data["LoginDay"]
		LoginTime.Value = data["LoginTime"]
		
		print('data exists')
		if tick() - LoginTime.Value >= Hours24 and tick() - LoginTime.Value <= Hours48 then -- Logged in after 24 but before 48 hours
			print("Logged in after 24 but before 48 hours")
			LoginTime.Value = tick()
			DailyLogin.Value = data["LoginDay"] + 1
			if DailyLogin.Value < 5 then
				task.wait(10)
				if player.PlayerGui.DailyLogin:FindFirstChild("Frame") then
					player.PlayerGui.DailyLogin.Frame.Visible = true
				end
				player:FindFirstChild("leaderstats"):FindFirstChild("Cash").Value += Awards["Day"..DailyLogin.Value]
				player.PlayerGui.DailyLogin.Frame:FindFirstChild("Day"..DailyLogin.Value).BackgroundColor3 = ClaimedColor
				task.wait(2.5)
				if player.PlayerGui.DailyLogin:FindFirstChild("Frame") then
					player.PlayerGui.DailyLogin.Frame.Visible = false
				end
			elseif DailyLogin.Value >= 5 then
				task.wait(10)
				if player:FindFirstChild("Tags"):FindFirstChild("Skilled").Value == false then
					player:FindFirstChild("Tags"):FindFirstChild("Skilled").Value = true
					if player.PlayerGui.DailyLogin:FindFirstChild("Frame") then
						player.PlayerGui.DailyLogin.Frame.Visible = true
					end
					DailyLogin.Value = 0
					player.PlayerGui.DailyLogin.Frame:FindFirstChild("Day5").BackgroundColor3 = ClaimedColor
					task.wait(2.5)
					player.PlayerGui.DailyLogin.Frame:FindFirstChild("Day1").BackgroundColor3 = NotClaimedColor
					player.PlayerGui.DailyLogin.Frame:FindFirstChild("Day2").BackgroundColor3 = NotClaimedColor
					player.PlayerGui.DailyLogin.Frame:FindFirstChild("Day3").BackgroundColor3 = NotClaimedColor
					player.PlayerGui.DailyLogin.Frame:FindFirstChild("Day4").BackgroundColor3 = NotClaimedColor
					player.PlayerGui.DailyLogin.Frame:FindFirstChild("Day5").BackgroundColor3 = NotClaimedColor
					task.wait(2.5)
					if player.PlayerGui.DailyLogin:FindFirstChild("Frame") then
						player.PlayerGui.DailyLogin.Frame.Visible = false
					end
				else
					DailyLogin.Value = 0
					if player.PlayerGui.DailyLogin:FindFirstChild("Frame") then
						player.PlayerGui.DailyLogin.Frame.Visible = true
					end
					player.PlayerGui.DailyLogin.Frame:FindFirstChild("Day5").BackgroundColor3 = ClaimedColor
					task.wait(2.5)
					player.PlayerGui.DailyLogin.Frame:FindFirstChild("Day1").BackgroundColor3 = NotClaimedColor
					player.PlayerGui.DailyLogin.Frame:FindFirstChild("Day2").BackgroundColor3 = NotClaimedColor
					player.PlayerGui.DailyLogin.Frame:FindFirstChild("Day3").BackgroundColor3 = NotClaimedColor
					player.PlayerGui.DailyLogin.Frame:FindFirstChild("Day4").BackgroundColor3 = NotClaimedColor
					player.PlayerGui.DailyLogin.Frame:FindFirstChild("Day5").BackgroundColor3 = NotClaimedColor
					player:FindFirstChild("leaderstats"):FindFirstChild("Cash").Value += 50000
					task.wait(2.5)
					if player.PlayerGui.DailyLogin:FindFirstChild("Frame") then
						player.PlayerGui.DailyLogin.Frame.Visible = false
					end
				end
			end
			
		elseif tick() - LoginTime.Value >= Hours48 then -- Logged in after 48 hours
			print("Logged in after 48 hours")
			LoginTime.Value = tick()
			DailyLogin.Value = 0
			task.wait(10)
			if player.PlayerGui.DailyLogin:FindFirstChild("Frame") then
				player.PlayerGui.DailyLogin.Frame.Visible = true
			end
			task.wait(2.5)
			player.PlayerGui.DailyLogin.Frame:FindFirstChild("Day1").BackgroundColor3 = ClaimedColor
			player.PlayerGui.DailyLogin.Frame:FindFirstChild("Day2").BackgroundColor3 = NotClaimedColor
			player.PlayerGui.DailyLogin.Frame:FindFirstChild("Day3").BackgroundColor3 = NotClaimedColor
			player.PlayerGui.DailyLogin.Frame:FindFirstChild("Day4").BackgroundColor3 = NotClaimedColor
			player.PlayerGui.DailyLogin.Frame:FindFirstChild("Day5").BackgroundColor3 = NotClaimedColor
			player:FindFirstChild("leaderstats"):FindFirstChild("Cash").Value += Awards["Day"..DailyLogin.Value]
			task.wait(2.5)
			if player.PlayerGui.DailyLogin:FindFirstChild("Frame") then
				player.PlayerGui.DailyLogin.Frame.Visible = false
			end
		elseif tick() - LoginTime.Value <= Hours24 then -- Logged in before 24 hours
			print("Logged in before 24 hours")
			
		end
	elseif not data then
		DailyLogin.Value = 1
		LoginTime.Value = tick()
		task.wait(10)
		if player.PlayerGui.DailyLogin:FindFirstChild("Frame") then
			player.PlayerGui.DailyLogin.Frame.Visible = true
		end
		player:FindFirstChild("leaderstats"):FindFirstChild("Cash").Value += Awards["Day"..DailyLogin.Value]
		player.PlayerGui.DailyLogin.Frame:FindFirstChild("Day"..DailyLogin.Value).BackgroundColor3 = ClaimedColor
		task.wait(2.5)
		if player.PlayerGui.DailyLogin:FindFirstChild("Frame") then
			player.PlayerGui.DailyLogin.Frame.Visible = false
		end
	end
end)


game.Players.PlayerRemoving:Connect(function(player)
	local DailyLoginKey = player.UserId .. "LoginData"
	local LoginTable = {}

	LoginTable["LoginDay"] = player:FindFirstChild("Login"):FindFirstChild("DailyLogin").Value
	LoginTable["LoginTime"] = player:FindFirstChild("Login"):FindFirstChild("LoginTime").Value


	local Success, errormessage = pcall(function()
		TableDataStore:SetAsync(DailyLoginKey, LoginTable)
	end)

	if Success then
		print(LoginTable)
	end
end)


game:BindToClose(function()
	for _, player in pairs(game.Players:GetChildren()) do

		local DailyLoginKey = player.UserId .. "LoginData"
		local LoginTable = {}

		LoginTable["LoginDay"] = player:FindFirstChild("Login"):FindFirstChild("DailyLogin").Value
		LoginTable["LoginTime"] = player:FindFirstChild("Login"):FindFirstChild("LoginTime").Value


		local Success, errormessage = pcall(function()
			TableDataStore:SetAsync(DailyLoginKey, LoginTable)
		end)

		if Success then
			print(LoginTable)
		end
	end
end)

From my testing it works fine, however I am not certain if there are any issues with using it on the server or in general. I saw there are some but don’t know if they would really affect me. I know os.time exists and if there are any big potential issues with tick() I will redo the script.

So are there are issues with tick() that would make me have to redo the script?

I’m aware of issues regarding daylights savings time and honestly if that somehow messes someone’s login streak up I don’t care

https://devforum.roblox.com/t/luau-recap-june-2020/632346

  • tick() sounds perfect - it has a high resolution (usually around 1 microsecond), and a well-defined baseline - it counts since UNIX epoch! Or, well, it actually doesn’t. On Windows, it returns you a variant of the UNIX timestamp in local time zone. In addition, it can be off by 1 second from the actual, real UNIX timestamp, and might have other idiosyncrasies on non-Windows platforms. We’re going to deprecate this in the future.

You should be using os.time or DateTime to handle Unix (real-world) timestamps.

1 Like