[ TIMER ] Need help getting timer to work correctly

Need help figuring out how I can make my counter below to still count upward if the player is jumping on my pad. Right now if the player is standing still on the part, then the counter works correctly as I want it. However, if the player jumps on the pad continuously, the timer gets reset thus giving the player an infinite chance on the pad.

What I am looking for is for the timer to continue to count upward if the player is still detected touching the part even if jumping. Otherwise reset the time. How can I go about doing this efficiently?

task.wait(1)

local counter = 0
local Part = script.Parent
local activeChar = nil
local Counter_Active = false

local params = OverlapParams.new()
params.FilterType = Enum.RaycastFilterType.Include
params.FilterDescendantsInstances = {}

---------------------------------------------------------------------
-- Touched
---------------------------------------------------------------------
Part.Touched:Connect(function(hit)
	if hit and hit.Parent then
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid then
			activeChar = hit.Parent
			params.FilterDescendantsInstances = {activeChar}

			print("Touched by:", activeChar.Name)

			Counter_Active = true
			counter = 0
		end
	end
end)


---------------------------------------------------------------------
-- Loop
---------------------------------------------------------------------
task.spawn(function()
	while true do
		task.wait(1)

		if Counter_Active == true and activeChar then

			local stillTouching = false
			local touching = workspace:GetPartsInPart(Part, params)

			for _, part in ipairs(touching) do
				if part:IsDescendantOf(activeChar) then
					stillTouching = true
					break
				end
			end

			if stillTouching then
				counter = counter + 1
			else
				counter = 0
				activeChar = nil
				Counter_Active = false
			end
		end

		print("Counter:", counter)
	end
end)
1 Like

Store the time the pad was last touched. When the pad is touched, check if the difference in now vs last touch is less than a certain threshold (you’ll need to tweak this value; maybe start with 2 seconds?). If it is less than, then re-use the same timer, otherwise reset it. Finally, store the new touch time and also remember to update it after the player disconnects from it.

You could, alternatively, spawn a second timer to do this task, but that’s a lot more boilerplate imo and less efficient.

Since the player can’t fly, no matter what, they need to touch/land on the pad at a very early point when they interact with it. Therefore you can use the Touched event as an “event starter”. There you take notice of which player touched the part based on the hit parameter. After fetching the player, you initiate a loop that runs constantly on an interval, that loop determines if the player is touching the pad by checking if the position of their HumanoidRootPart is within the region of the pad on the x, z axis (ignoring height, or if you want you can add limits there as well). When the player gets outside the region, you terminate the loop. Every time the Touched event fires, you check if there’s a loop currently running for that player in that specific pad, if it does then you ignore it, else you initiate it.

You can modify this as you like, for example you can have a loop checking for multiple players, or multiple pads, at once. However the main behavior described above should remain the same.

1 Like

Can you show some code please?

Here’s something I was able to quickly come up with:

local pad: BasePart = script.Parent
local counter = 0

local touching: {Player} = {}
local listenerRunning = false 

local function withinPad(root: BasePart): boolean
	local clone = root:Clone()
	clone.Position = Vector3.new(root.Position.X, pad.Position.Y, root.Position.Z)
	clone.Anchored = true 
	clone.Name = "temp"
	clone.Parent = workspace 
	local within = table.find(clone:GetTouchingParts(), pad)
	clone:Destroy()
	return not not within 
end

local function checkPlayer(player: Player): boolean
	local character = player.Character 
	if not character then return false end
	local root: BasePart = character:FindFirstChild("HumanoidRootPart")
	if not root then return false end
	return withinPad(root)
end

local function initiateListener(): ()
	listenerRunning = true
	while task.wait(1) do
		for _, player in pairs(touching) do
			if checkPlayer(player) then continue end
			table.remove(touching, player)
			print(player.Name.." stopped touching!")
		end
		counter = #touching > 0 and counter+1 or 0
		print("Current value of counter: "..counter)
		if counter == 0 then
			listenerRunning = false 
			break 
		end
	end
end

pad.Touched:Connect(function(hit)
	if not hit.Parent:FindFirstChild("Humanoid") then return end
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if not player then return end
	if table.find(touching, player) then return end
	table.insert(touching, player)
	if not listenerRunning then initiateListener() end
	print(player.Name.." started touching!")
end)

The way it checks that a player is on the pad is that it clones their root part, it positions the clone at the same height as the pad, and then checks if the pad is within that clone (they intersect). Basically it completely ignores height, but you can modify this however you like.

1 Like

make a invisible hitbox check on the x, y + 10, z axis so even if the player jumps they should still be in bounds because of y +10 for example

local plr = game.Players.LocalPlayer
repeat task.wait() until plr.Character
local char = plr.Character

local HeightOfHitBox = 100 -- the height of which the y hitbox will stretch upwards (used to capture jumps easily since hitbox is stretch upwards. i set this to 100 but you can play around with this value)
local pad = workspace:WaitForChild("Pad")

while task.wait() do
	if char then
		local humP = char:FindFirstChild("HumanoidRootPart")
		if humP and humP.Position.X >= (pad.Position.X - (pad.Size.X/2)) and humP.Position.X <= (pad.Position.X + (pad.Size.X/2)) and humP.Position.Z <= (pad.Position.Z + (pad.Size.Z/2)) and humP.Position.Z >= (pad.Position.Z - (pad.Size.Z/2)) and humP.Position.Y <= pad.Position.Y + HeightOfHitBox then
			print("IN PAD BOUNDS EVEN IF JUMPING PLAY TIMER")
		else
			print("player not in bounds dont play timer")
		end
	end
end

this should work with what your saying but i may have misread the problem so please let me know if im helping in the right way or not.

1 Like

To add onto this if you wanted it to make sure the player initiates any kind of touch before running this hitbox loop then just add a .touched function that sets a variable like StartLoop to true on touch and if there out of hitbox then set it back to false. Hope this helps

1 Like
local plr = game.Players.LocalPlayer
repeat task.wait() until plr.Character
local char = plr.Character

local HeightOfHitBox = 100 -- the height of which the y hitbox will stretch upwards (used to capture jumps easily since hitbox is stretch upwards. i set this to 100 but you can play around with this value)
local x_hitbox_extend = 5 -- OPTIONAL IF YOU WANT THE HITBOX TO EXTEND OFF THE PAD
local z_hitbox_extend = 5 -- OPTIONAL IF YOU WANT THE HITBOX TO EXTEND OFF THE PAD

local pad = workspace:WaitForChild("Pad")

local startloop = false

spawn(function() -- you can also couritine this its recommended because you have more control of the thread with couritine but for this ill just use a spawn function
	while task.wait() do
		if char and startloop then
			local humP = char:FindFirstChild("HumanoidRootPart")
			if humP and humP.Position.X >= (pad.Position.X - ((pad.Size.X/2)+x_hitbox_extend)) and humP.Position.X <= (pad.Position.X + ((pad.Size.X/2)+x_hitbox_extend)) and humP.Position.Z <= (pad.Position.Z + ((pad.Size.Z/2)+z_hitbox_extend)) and humP.Position.Z >= (pad.Position.Z - ((pad.Size.Z/2)+z_hitbox_extend)) and humP.Position.Y <= pad.Position.Y + HeightOfHitBox then
				print("IN PAD BOUNDS EVEN IF JUMPING PLAY TIMER")
			else
				startloop = false
				print("player not in bounds dont play timer")
			end
		end
	end
end)

pad.Touched:Connect(function(hit) -- touch logic
	local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if plr then
		startloop = true
	end
end)

this is with more customization and that extra touch logic i mentioned. play around with these scripts and ideas and tune it to your liking. but this should be what your looking for

1 Like

Thank you for the help. Both solutions work well for what I am trying to do. I am going to test each method and see which works the best for what I am looking for.

Method 1:

Method 2:

1 Like

ya man ofc good luck on what your doing

1 Like

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