Make points dont add so fast

local RunService = game:GetService("RunService")
local PointA = game.Workspace.A
local PointB = game.Workspace.B
local PointC = game.Workspace.C
local Raiders = game.Teams.Raiders
local Defenders = game.Teams.Defenders
local captureTime = 5
local capturedBy = nil

local function updatePointsLabel(player)
	local PlayerGUI = player:FindFirstChild("PlayerGui")
	if PlayerGUI then
		local pointsLabel = PlayerGUI:FindFirstChild("RobloxStyledTerminal")
		if pointsLabel then
			local Frame = pointsLabel:FindFirstChild("LeftFrame")
			if Frame then
				local pointCount = Frame:FindFirstChild("PointCount")
				if pointCount then
					pointCount.Text = tostring(PointA.Points.Value)
					print("changed this thing")
				end
			end
		end
	end
end

local function raycastThing(part, team)
	local capturedSeconds = part:FindFirstChild("capturedSeconds")
	local raycastOrigin = part.Position
	local raycastDirection = raycastOrigin + Vector3.new(10, 10, 10)

	while capturedSeconds.Value < captureTime do
		local result = workspace:Raycast(raycastOrigin, raycastDirection, nil, nil)
		if result then
			if game.Players:GetPlayerFromCharacter(result.Instance.Parent).TeamColor == Raiders.TeamColor then
				capturedSeconds.Value = capturedSeconds.Value + 1
				print("working")
			end
		end
		RunService.Heartbeat:Wait()
	end
end


PointA.Touched:Connect(function(part)
	if PointA.Captureable.Value == true then
		local player = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
		print("found player")
		if player then
			local PlayerGUI = player:FindFirstChild("PlayerGui")
			print("found playergui")
			if player.TeamColor == Raiders.TeamColor and (capturedBy == nil or capturedBy == "Def") then
				raycastThing(PointA, Raiders)
				if PointA.capturedSeconds.Value == captureTime then
					while PointA.Captureable.Value == true and PointA.Points.Value < 10 do
						PointA.Points.Value += 1
						RunService.Heartbeat:Wait()
						if capturedBy == "Def" then
							break
						end
					end
				end
				if PointA.Points.Value == 10 then
					capturedBy = nil
					PointA.Captureable.Value = false
					PointB.Captureable.Value = true
				end
			elseif player.TeamColor == Defenders.TeamColor and capturedBy == "Raid" then
				capturedBy = "Def"
				while PointA.Captureable.Value == true and (PointA.Points.Value < 10 and PointA.Points.Value > 0) do
					PointA.Points.Value -= 1
					RunService.Heartbeat:Wait()
				end
			end
			updatePointsLabel(player) -- Update the points label immediately after the change
			PointA.Points.Changed:Wait() -- Wait for the next change in PointA.Points.Value
			updatePointsLabel(player) -- Update the points label again after the next change
		end
	end
end)

purpose of this script is to check when player capture terminal. If it is captured (5 seconds), points are getting added blah blah

Problem is, when I step on part, values of capturedSeconds and PointA.Points are automatically added, how can I prevent this?

1 Like

You can save the time when the part is touched with os.clock() then check if the time passed is more than five seconds.

local lastTouched -- make a variable to store the time when the part was last touched
part.Touched:Connect(function()
	if lastTouched then -- if the part was touched before
		if os.clock() - lastTouched < 5 then return end -- if the time between this touch and the last touch is less than five, then we do nothing and don't save this time as the last touched
	end
	lastTouched = os.clock() -- store the last touched
end)
2 Likes

Wait does this kind of work like a debounce and if so which one is better if you know

in what place in script should i place it?

local RunService = game:GetService("RunService")
local PointA = game.Workspace.A
local PointB = game.Workspace.B
local PointC = game.Workspace.C
local Raiders = game.Teams.Raiders
local Defenders = game.Teams.Defenders
local captureTime = 5
local capturedBy = nil
local lastTouched = nil

local function updatePointsLabel(player)
	local PlayerGUI = player:FindFirstChild("PlayerGui")
	if PlayerGUI then
		local pointsLabel = PlayerGUI:FindFirstChild("RobloxStyledTerminal")
		if pointsLabel then
			local Frame = pointsLabel:FindFirstChild("LeftFrame")
			if Frame then
				local pointCount = Frame:FindFirstChild("PointCount")
				if pointCount then
					pointCount.Text = tostring(PointA.Points.Value)
					print("changed this thing")
				end
			end
		end
	end
end

local function raycastThing(part, team)
	local capturedSeconds = part:FindFirstChild("capturedSeconds")
	local raycastOrigin = part.Position
	local raycastDirection = raycastOrigin + Vector3.new(10, 10, 10)

	while capturedSeconds.Value < captureTime do
		local result = workspace:Raycast(raycastOrigin, raycastDirection, nil, nil)
		if result then
			if game.Players:GetPlayerFromCharacter(result.Instance.Parent).TeamColor == Raiders.TeamColor then
				capturedSeconds.Value = capturedSeconds.Value + 1
				print("working")
			end
		end
		RunService.Heartbeat:Wait()
	end
end


PointA.Touched:Connect(function(part)
	if PointA.Captureable.Value == true then
		local player = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
		print("found player")
		if player then
			local PlayerGUI = player:FindFirstChild("PlayerGui")
			print("found playergui")
			if player.TeamColor == Raiders.TeamColor and (capturedBy == nil or capturedBy == "Def") then
				if lastTouched then
					if os.clock() - lastTouched < 5 then return end
					raycastThing(PointA, Raiders)
					if PointA.capturedSeconds.Value == captureTime then
						while PointA.Captureable.Value == true and PointA.Points.Value < 10 do
							PointA.Points.Value += 1
							RunService.Heartbeat:Wait()
							if capturedBy == "Def" then
								break
							end
						end
					end
					if PointA.Points.Value == 10 then
						capturedBy = nil
						PointA.Captureable.Value = false
						PointB.Captureable.Value = true
					end
				elseif player.TeamColor == Defenders.TeamColor and capturedBy == "Raid" then
					capturedBy = "Def"
					while PointA.Captureable.Value == true and (PointA.Points.Value < 10 and PointA.Points.Value > 0) do
						PointA.Points.Value -= 1
						RunService.Heartbeat:Wait()
					end
				end
			end
			updatePointsLabel(player) -- Update the points label immediately after the change
			PointA.Points.Changed:Wait() -- Wait for the next change in PointA.Points.Value
			updatePointsLabel(player) -- Update the points label again after the next change
		end
	end
	lastTouched = os.clock()
end)

Changed it to this, still the same happening

Put

lastTouched = os.clock()

Right after

if os.clock() - lastTouched < 5 then return end

After I did it, raycast stopped working

Can you please help me with that?

Im still trying to figure it out