Help with timer increasing and decreasing with run service

Hi, I’m trying to create a system where depending on where if your character is looking back, a timer will count up to 5, once it reaches 5 it will cause an event to happen, on the other hand if you look fowards, the timer will start to tick down, and looking back again will cause it to tick back up from that number.

The problem is I honestly have no clue how I would do that, here’s my code.

local CheckTime
local LookingBack = false

RunService.RenderStepped:Connect(function()
	if HumanoidRootPart.CFrame.RightVector.X > 0.5 then
		LookingBack = true
		if os.time() - CheckTime >= 5 then
			print("Reached")
		end
	else
		CheckTime = os.time()
		LookingBack = false
	end
end)

Does anybody know how to make this work?

1 Like

You shouldn’t actually be using time here (because it would be far more complicated than necessary). You should instead be adding/removing the delta time parameter that gets passed into the function.

runservice.renderstepped:Connect(function(deltaTime)

Just add or subtract deltaTime from or to your counter to get the desired effect.

1 Like

You could Probably use the DeltaTime Property from the RunService.RenderStepped Connection:

RunService.RenderStepped:Connect(function(DeltaTime)

(I was going to say this but @tlr22 beat me to it)
And Add or Subtract DeltaTime when a specific Condition is met, DeltaTime would basically be the Difference in Time between Frames, and would be based on your fps

-- Before Connection:
local EventStarted = false -- to check if to start the Event
local Time = 0 -- to Count the Time

local x = 5 -- the number to go towards (in seconds)

-- During RenderStepped:

LookingBack = (HumanoidRootPart.CFrame.RightVector.X > 0.5)
Time = if LookingBack then
    math.min(x, Time + DeltaTime)
else
    math.max(0, Time - DeltaTime)

-- The Following code above will apply specific values if a certain condition
-- is met.

-- LookingBack will be set to a Boolean, as we are saying that RightVector.X > .5
-- which can be true, or false, which is what will be given to LookingBack

-- Time will apply a Value when a Condition is met, which in this case
-- If they are Looking Back, it will add up, math.min is to make sure the number
-- doesnt go over a specific amount, it looks for the smallest number, and if
-- Time is larger than x, it will return as x because its the smallest value

-- math.max would be the exact opposite, instead we are preventing the Value
-- from going below 0



if Time == 5 and not EventStarted then 
    -- This to check if the Conditions are met to fire the Event
    -- If Time has Reached its goal, and if the Event hasnt started yet


    EventStarted = true
    -- you can then start the Event
    -- This check is to make sure it doesnt fire constantly, if thats what you want
else
    EventStarted = false
end

If you want the Time to added up depending on a Duration given, you will need to Divide DeltaTime by the Duration, thats if you going for a number from 0 to 1, if not, just change x to a specific amount.

Its pretty basic, but It should get the job done.

2 Likes

Thank you, this was super confusing to me, really appreciate it! :smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.