I am doing a script for capturing a point, which you can see in multiple games: the one where you stand in for a while to capture it for your team. But while I was doing it, a problem occured. When you get it captured, atleast in Play Solo mode, and you leave the capture point, a script gets "stuck". This is that script:

local player = game.Players.LocalPlayer

local RunService = game:GetService("RunService")

local Terminal = workspace:WaitForChild("Terminal")
local TerminalPart = Terminal.PrimaryPart or Terminal:WaitForChild("TerminalPart")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EnteredTerminal = ReplicatedStorage:WaitForChild("EnteredTerminal")
local LeftTerminal = ReplicatedStorage:WaitForChild("LeftTerminal")

local inside = false

local MAXDISTANCE = 3

while wait() do
	if not player.Neutral and player.Character then
		local character = player.Character
		
		local torso = character:FindFirstChild("Torso")
		local humanoid = character:FindFirstChild("Humanoid")
		
		if not inside and torso and (Vector3.new(TerminalPart.CFrame.X,0,TerminalPart.CFrame.Z) - Vector3.new(torso.CFrame.X,0,torso.CFrame.Z)).magnitude <= MAXDISTANCE and humanoid and humanoid.Health > 0 then
			inside = true
			EnteredTerminal:FireServer()
		elseif inside and torso and ((Vector3.new(TerminalPart.CFrame.X,0,TerminalPart.CFrame.Z) - Vector3.new(torso.CFrame.X,0,torso.CFrame.Z)).magnitude > MAXDISTANCE or humanoid and humanoid.Health <= 0) then
			inside = false
			LeftTerminal:FireServer()
		end
	end
end

This script fires RemoteEvents when players enter or leave the capture point. And I tested it, and it works fine when you leave the capture point during the capture. But when you have captured it, and you leave, as I told before, the game pauses. This is the main script for capturing:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local EnteredTerminal = Instance.new("RemoteEvent")
EnteredTerminal.Name = "EnteredTerminal"
EnteredTerminal.Parent = ReplicatedStorage

local LeftTerminal = Instance.new("RemoteEvent")
LeftTerminal.Name = "LeftTerminal"
LeftTerminal.Parent = ReplicatedStorage

local ServerStorage = game:GetService("ServerStorage")
local start = ServerStorage:WaitForChild("Start")
local stop = ServerStorage:WaitForChild("Stop")
local running = ServerStorage:WaitForChild("Running")

local startCapture = ServerStorage:WaitForChild("StartCapture")
local updateSpeed = ServerStorage:WaitForChild("UpdateSpeed")
local stopCapture = ServerStorage:WaitForChild("StopCapture")
local captureFinished = ServerStorage:WaitForChild("CaptureFinished")
local capturing = ServerStorage:WaitForChild("Capturing")

local currentOwner = nil
local PlayersInTerminal = {}

function GetTeamCount(team)
	local teamCount,restCount = 0,0
	
	for player in pairs(PlayersInTerminal) do
		if player.Team == team then
			teamCount = teamCount + 1
		else
			restCount = restCount + 1
		end
	end
	
	return teamCount,restCount
end

function UpdateLargestTeam()
	
	local TeamCounts = {}
	local LargestTeam
	
	for player in pairs(PlayersInTerminal) do
		if not player.Neutral then
			if not TeamCounts[player.Team] then
				TeamCounts[player.Team] = 0
			end
			
			TeamCounts[player.Team] = TeamCounts[player.Team] + 1
		end
	end
	
	local highestCount = 0
	local highestTeams = 0
	
	for team,count in pairs(TeamCounts) do
		if count > highestCount then
			print(team.Name)
			highestCount = count
			highestTeams = 1
			LargestTeam = team
		elseif count == highestCount then
			highestTeams = highestTeams + 1
		end
	end
	
	return highestTeams <= 1 and LargestTeam or highestTeams > 1 and true or not LargestTeam and false
end

function StartCapture(team,length,speed)
	if team and type(team) == "userdata" and team:IsA("Team") and team ~= currentOwner then
		local success
		
		print("Team ".. team.Name.. " is capturing!")
		
		startCapture:Invoke(team,length,speed)
		
		local success = captureFinished.Event:wait()
		
		if success then
			currentOwner = team
		end
		
		capturingTeam = nil
		
		print(success)
		return success
	end
end

function OnEntered(player)
	
	PlayersInTerminal[player] = true
	
	local currentLargestTeam = UpdateLargestTeam()
		
	capturingTeam = capturingTeam or currentLargestTeam
	
	local teamCount,restCount = GetTeamCount(capturingTeam)
	local speed = teamCount-restCount
	
	if not capturing.Value then
		local success = StartCapture(capturingTeam,12,speed)
		
		while not success and capturingTeam ~= currentOwner do
			local currentLargestTeam = UpdateLargestTeam()
			capturingTeam = capturingTeam or currentLargestTeam
			
			teamCount,restCount = GetTeamCount(capturingTeam)
			local newSpeed = teamCount-restCount
			
			success = StartCapture(capturingTeam,12,newSpeed)
		end
		
		if capturingTeam == currentOwner then
			capturingTeam = nil
		end
	else
		updateSpeed:Invoke(speed)
	end
end

function OnLeft(player)
	
	
	if PlayersInTerminal[player] then
		PlayersInTerminal[player] = nil
	end
	
	local currentLargestTeam = UpdateLargestTeam()
	
	local teamCount,restCount = GetTeamCount(capturingTeam)
	local speed = teamCount-restCount
	
	if not capturing.Value then
		local success = StartCapture(capturingTeam,12,speed)
		
		while not success and capturingTeam ~= currentOwner do
			local currentLargestTeam = UpdateLargestTeam()
			capturingTeam = capturingTeam or currentLargestTeam
			
			teamCount,restCount = GetTeamCount(capturingTeam)
			local newSpeed = teamCount-restCount
			
			success = StartCapture(capturingTeam,12,newSpeed)
		end
		
		if capturingTeam == currentOwner then
			capturingTeam = nil
		end
	else
		updateSpeed:Invoke(speed)
	end
end

EnteredTerminal.OnServerEvent:connect(OnEntered)
LeftTerminal.OnServerEvent:connect(OnLeft)

(Please don't take a look at how weirdly it's written, since I am not done with it) If you have a proper solution for this problem, please post it as an answer.

Thank you,

SwaggyDuckie

3 comments

0

Errors? Have you tried debugging

0

There are no errors, and I have tried debugging. Debugging is hard though, because the script that leaves hanging has an infinite while loop. Stepping the code just kept running the infinite while loop.

0

It asks me to break or kill the script that leaves hanging. I think I've had this problem before and if there's no other way, changing the order of stuff happening in the script that detects entering and leaving should work. The breaking didn't happen in an earlier version that was written differently.