Detect if player has been on part for X seconds for the operation to proceed

Hello

  1. What do you want to achieve? I am currently making a CTP system of my own and I want to detect if a player has been on the Point part for X seconds for the capture to be success, and for it to cancel if the player leaves before the X seconds

  2. What is the issue? I tried using variables like local InProcessOfCapture to determine the process, and if the variable was true and the TouchEvent ended while the player was capturing, it would be cancelled and the Point would return to its previous unclaimed state

  3. What solutions have you tried so far? I looked across da forum and found nothing related to my matter, I couldn’t think of another way to solve my issue other than touchended

local inProcessOfCapture = false

local PointA = script.Parent
local Debounce = false

PointA.Touched:connect(function(hit)
	if not game.Players:GetPlayerFromCharacter(hit.Parent) then return end
	local char = hit.Parent
	local MainPlayerTeam = char.TeamColor

			if MainPlayerTeam == BrickColor.new("Really blue") then
				if Debounce == false and PointA.BrickColor == BrickColor.new("Really red") or PointA.BrickColor == BrickColor.new("Institutional white") then
					script.Parent.GUILOL.Enabled = true
			Debounce = true
			local inProcessOfCapture = true
					wait(3)
					PointA.BrickColor = BrickColor.new("Really blue")
			script.Parent.GUILOL.Enabled = false
			local inProcessOfCapture = false
					Debounce = false
				end
			elseif MainPlayerTeam == BrickColor.new("Really red") then
				if Debounce == false and PointA.BrickColor == BrickColor.new("Really blue") or PointA.BrickColor == BrickColor.new("Institutional white") then
			script.Parent.GUILOL.Enabled = true
			local inProcessOfCapture = true
					Debounce = true
					wait(3)
					PointA.BrickColor = BrickColor.new("Really red")
			script.Parent.GUILOL.Enabled = false
			local inProcessOfCapture = false
					Debounce = false
				end
			end

PointA.TouchEnded:connect(function(hit)
	if hit.Parent == char then
		if inProcessOfCapture == true then
			Debounce = false
			PointA.BrickColor = BrickColor.new("Institutional white")
			script.Parent.GUILOL.Enabled = false
		end
	end
end)
end)

It was step on part to instantly capture (not including the wait(5)) before I added the Touch and TouchEnded events, were these wrong functions

You can use raycasts to detect the player on the part. Then, use tick() to determine how long they have been on the part.

1 Like

The touch event is known to be quite buggy in my opinion. For this, I think using the ZonePlus Module: Method - ZonePlus
There are many advantages to using Z+ here, but in your case it’ll help you accurately determine if they are in it.
Also, it’s :Connect not :connect.

1 Like

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