Does the tick() value ever change when stored in a variable?

Say I have something like this local Timenow = tick() , does this value change as the game goes on? Since tick() returns the ever increasing seconds since january 1st 1970.

1 Like

Depends. If you put that variable in a function and you call that function at different times, then Timenow will have different answers. But if you put it outside a function, it will stay the same.

No, value will not change.
but you can do something like this:

-- This code will not work.
local Timenow = tick()

while wait(1) do
  print("Time: "..tostring(Timenow))
end
-- This code will work.
while wait(1) do
  local Timenow = tick()
  print("Time: "..tostring(Timenow))
end

if it doesn’t change then may I ask how does something like this

local CurrentTime = 0
local PreviousTime = 0

UIS.InputBegan:Connect(function(Input,Istyping)
	if Istyping then
		return
	elseif Input.UserInputType == Enum.UserInputType.MouseButton1 then
		
		CurrentTime = tick()
		local TimePassed = tick() - PreviousTime
		print(TimePassed)
                if TimePassed < 1 then 
                  RemoteEvent:Fire()
	end
	end
end)

RemoteEvent.OnClientEvent:Connect(function()
     PreviousTime = CurrentTime
end)

works? Specifically asking about the time math.

It’s updating the time each time you left click. Whenever the remote is fired it updates the variable to the current time.

Oh I see, what’s the purpose of setting the PreviousTime to the CurrentTime then?

This looks like a debounce system to me, preventing remoteevent from getting fired more than once per second.

1 Like

imagine this,
you are gonna create a variable,
you are going to set the value of that variable to 2
the type of the value is a number

you are gonna create another variable
you are going to set the value of that variable to tick()
what tick does is returns the the seconds since 1/1/1970 in NUMBERS
the type of the value is a number

the value wont change unless you set another value, meaning using tick() on any variable will stay the same value unless changed again

Anything stored as a variable will maintain it’s state from when it was stored or whatever was returned.
A variable that does not change will become a constant, so if you weren’t to change Timenow that’s what it would be, if you were to change it during the runtime of the program it would become an upvalue.
Note that as always, exploiters can modify or change any constants or upvalues on the client so using this a method of an anti exploit would not be guaranteed to work for some.