Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What other then a infinite loop causes "hanging code"?

Sometimes, we get this warning or so by Roblox Studio saying that a code has been left hanging for a while telling us to break it. This normally occurs during infinite loops. But the thing is, I did NOT use an infinite loop and I received this warning.

I have 2 question:

What causes "hanging code"?

and

What can be done to fix "hanging code"?

Local Script:


local function ManagePlayerScripts(Disable)
	if Disable then
		Char['Gear Equip Handler'].Disabled = true
		if plr:WaitForChild('PlayerStatus').WeaponEquipped.Value then				 
			RE.EquipGear:FireServer('Weapon') -- Force Unequip
		end
		if plr:WaitForChild('PlayerStatus').ShieldEquipped.Value then
			RE.EquipGear:FireServer('Shield') -- force unequip
		end
	else
		plr['Gear Equip Handler'].Disabled = false
	end
end

local function ManageBillBoardGuis(Var)
	wait(0.25)
	if Var then
		for _, Gui in pairs(PlayerGui:GetChildren()) do
			if Gui:IsA('BillboardGui') then
				Gui.PlayerToHideFrom = plr
			end
		end
	else
		for _, Gui in pairs(PlayerGui:GetChildren()) do
			if Gui:IsA('BillboardGui') then
				Gui.PlayerToHideFrom = nil
			end
		end
	end
end



local function TypeWriterEffect(ThingToSay)
	PlayerGui.InteractionGui.ChatFrame.DisplayText.Text = ''
	PlayerGui.InteractionGui.ChatFrame.Visible = true	
	for i = 1, #ThingToSay do
		wait()
    	PlayerGui.InteractionGui.ChatFrame.DisplayText.Text = string.sub(ThingToSay, 1, i)
		if PlayerGui.InteractionGui.ChatFrame.Visible == false or not IsOpened then
			break -- So basically I wanna stop the typing animation
		end
	end
	wait(3)
	PlayerGui.InteractionGui.ChatFrame.Visible = false
	PlayerGui.InteractionGui.ChatFrame.DisplayText.Text = ''
	IsOpened = false
	ManageBillBoardGuis(false)	
end

local function ManageControlScript(Disable)
	if Disable then
		for _, v in pairs(plr.PlayerScripts:GetChildren()) do
    		if v.Name == "ControlScript" then
        		v.Disabled = true
    		end 
		end
	else
		for _, v in pairs(plr.PlayerScripts:GetChildren()) do
    		if v.Name == "ControlScript" then
        		v.Disabled = false
    		end 
		end
	end
end

local function MovePlayerToNPC(npc, Pos)
	ManageControlScript(true)
	Humanoid:MoveTo(Pos)
	Humanoid.MoveToFinished:Wait()
	ManageControlScript(false)
	print("movefinished")
	ManagePlayerInput(false)
	RotatePlayerToNpc(npc)
	
end

local function ReturnCamera()
	
	local Info = TweenInfo.new(0.5)
	
	local Direc = OriginalCameraOffset - OriginalCameraOffset.p
	local GoalCF = Direc + (Char.HumanoidRootPart.Position + OriginalCameraOffset.p)
	
	local Goal = {["CFrame"] = GoalCF}
	local Tween = TweenService:Create(CurrentCamera,Info,Goal)
	
	Tween:Play()
	Tween.Completed:Wait()
	CurrentCamera.CameraType = Enum.CameraType.Custom
	
end

local function OpenGuilds()
	PlayerGui.GuildGui.Guild.Visible = true
end

local function EndInteraction()
	
	ReturnCamera()
	ManagePlayerInput(true)
	ManageBillBoardGuis(false)
	ManageControlScript(false)
	
end

local List = {
	workspace.Mobs.Neutral["News Bot"];
	workspace.BuiltTerrain.Cave;
	workspace.Mobs.AI.Emily;
	workspace.Mobs.AI.Hanzo;
	workspace.Buildings["Guild House"].Door;
	workspace["Building Insides"].GuildHouseInside.Door;
	workspace.Mobs.Neutral["Guild Master: Chi"];
}

print('Started already and running')

local function AddInteract(Thing)
	
	local InteractGui = ReplicatedStorage.Other.InteractGui
	InteractGui.BackFrame.InteractButton.Text = ''
	
	local Clone = InteractGui:Clone()
	Clone.Parent = PlayerGui
	Clone.Adornee = Thing.HumanoidRootPart
	Clone.BackFrame.InteractButton.Text = 'Click To Interact With '..Thing.Name
	
	print('Interactive')
	
	--When player clicks to interact with bot:
	Clone.BackFrame.InteractButton.MouseButton1Down:Connect(function()
		
		if Clone.Adornee.Parent.Name == 'News Bot' and not IsOpened then 
			--Clone.BackFrame:TweenSize(UDim2.new(0,0,0,0),'In','Sine',0.25,true)						
			IsOpened = true
			ManageBillBoardGuis(true)
			
			--ChangeCameraToLookAtNPC(workspace.CameraParts.LookAtNewsBot, Thing)
			LookAtNPC(Thing, -10, 8, -10)
			MovePlayerToNPC(Thing, (Thing.HumanoidRootPart.CFrame*CFrame.new(0,0,-7)).p)
			
			TypeWriterEffect('Beep boop beep boop, everyone is trying to hurt me :c')
			
			wait(0.25)
			EndInteraction()
		elseif Clone.Adornee.Parent.Name == 'Emily' and not IsOpened then
			IsOpened = true
			ManageBillBoardGuis(true)
			Thing.HumanoidRootPart.Anchored = true
			LookAtNPC(Thing, -10,8,-10)
			MovePlayerToNPC(Thing, (Thing.HumanoidRootPart.CFrame*CFrame.new(0,0,-7)).p)		
			
			TypeWriterEffect("Hanzo is so handsome... but I'm too shy to approach")
			
			wait(0.25)
			EndInteraction()
			Thing.HumanoidRootPart.Anchored = false
		elseif Clone.Adornee.Parent.Name == 'Hanzo' and not IsOpened then
			IsOpened = true
			ManageBillBoardGuis(true)
			Thing.HumanoidRootPart.Anchored = true
			LookAtNPC(Thing, -10,8,-10)
			MovePlayerToNPC(Thing, (Thing.HumanoidRootPart.CFrame*CFrame.new(0,0,-7)).p)		
			
			TypeWriterEffect("I really like Emily, but I don't know why she keeps running away :c.")
			
			wait(0.25)
			EndInteraction()
			Thing.HumanoidRootPart.Anchored = false
		elseif Clone.Adornee.Parent.Name == 'Bob' and not IsOpened then
			IsOpened = true
			ManageBillBoardGuis(true)
			Thing.HumanoidRootPart.Anchored = true
			LookAtNPC(Thing, -10,8,-10)
			MovePlayerToNPC(Thing, (Thing.HumanoidRootPart.CFrame*CFrame.new(0,0,-7)).p)		
			
			TypeWriterEffect("I always wanted to be strong, but these new mobs are torture.")
			
			wait(0.25)
			EndInteraction()
			Thing.HumanoidRootPart.Anchored = false
		elseif Clone.Adornee.Parent.Name == 'Max' and not IsOpened then
			IsOpened = true
			ManageBillBoardGuis(true)
			Thing.HumanoidRootPart.Anchored = true
			LookAtNPC(Thing, -10,8,-10)
			MovePlayerToNPC(Thing, (Thing.HumanoidRootPart.CFrame*CFrame.new(0,0,-7)).p)		
			
			TypeWriterEffect("The new Green Scimitar is cool...")
			
			wait(0.25)
			EndInteraction()
			Thing.HumanoidRootPart.Anchored = false
		elseif Clone.Adornee.Parent.Name == 'Guild Master: Chi' and not IsOpened then
			IsOpened = true
			ManageBillBoardGuis(true)
			Thing.HumanoidRootPart.Anchored = true
			LookAtNPC(Thing, -10,8,-10)
			MovePlayerToNPC(Thing, (Thing.HumanoidRootPart.CFrame*CFrame.new(0,0,-7)).p)		
			
			TypeWriterEffect("Hey there! I'm the guild master here, call me Chi. Let's take a look at your stats!")
			
			wait(0.25)
			EndInteraction()
			Thing.HumanoidRootPart.Anchored = false
			OpenGuilds()
			ManageControlScript(true)
		elseif Clone.Adornee.Parent.Name == 'Cave' and not IsOpened then
						
			
			local ScreenGui = Instance.new("ScreenGui")
			ScreenGui.Name = 'TransitionGui'
			ScreenGui.Parent = PlayerGui
	
			local BlackScreen = Instance.new("Frame")
			BlackScreen.BackgroundColor3 = Color3.fromRGB(0,0,0)
			BlackScreen.Size = UDim2.new(2,0,2,0)
			BlackScreen.BorderSizePixel = 50
			BlackScreen.AnchorPoint = Vector2.new(0.5,0.5)
			BlackScreen.Position = UDim2.new(0.5,0,0.5,0)
			BlackScreen.ZIndex = 1000
			BlackScreen.BackgroundTransparency = 1
			BlackScreen.Visible = true
			BlackScreen.Parent = ScreenGui
		
			for i = 1, 0, -0.01 do
				BlackScreen.BackgroundTransparency = i
				wait()
			end 
			
			local S = Instance.new("ScreenGui")
			S.Parent = PlayerGui
			
			local ImageLabel = Instance.new("ImageLabel")
			ImageLabel.Size = UDim2.new(1,0,1,0)
			ImageLabel.Image = 'rbxgameasset://Images/Blox Online Loading Screen Tree Of Life'
			ImageLabel.BackgroundTransparency = 0
			ImageLabel.ZIndex = 2
			ImageLabel.Parent = S
	
			for i = 0, 1, 0.01 do
				BlackScreen.BackgroundTransparency = i
				wait()
			end
				
			ScreenGui:Destroy()
			
			RE.Teleport:FireServer(2022419420)
		elseif Clone.Adornee.Parent.Parent.Name == 'Guild House' and not IsOpened then
			LookAtNPC(Thing, 10,8,20)
			MovePlayerToNPC(Thing, (Thing.HumanoidRootPart.CFrame*CFrame.new(0,0,7)).p)
			wait(0.25)			
			EndInteraction()			
			CreateTransition(workspace['Building Insides'].GuildHouseInside.TeleportPart.CFrame)			
			ManagePlayerScripts(true)		
		elseif Clone.Adornee.Parent.Parent.Name == 'GuildHouseInside' and not IsOpened then
			LookAtNPC(Thing, -10,8,-20)
			MovePlayerToNPC(Thing, (workspace['Building Insides'].GuildHouseInside.TeleportPart.CFrame).p)
			wait(0.25)
			EndInteraction()
			CreateTransition(workspace.Buildings["Guild House"].Door.HumanoidRootPart.CFrame*CFrame.new(0,0,7))	
			ManagePlayerScripts(false)		
		end
		
--		wait(0.25)
--		ManagePlayerInput(true)
		--Clone.BackFrame:TweenSize(UDim2.new(1,0,1,0),'Out','Sine',0.25,true)
	end)
	
end

local function SetUp()
	print('Started')
	for _, Thing in pairs(workspace:GetDescendants()) do
		for _, Listed in pairs(List) do
			print('ye')
			if Thing:IsA('Model') and Thing:FindFirstChild('HumanoidRootPart') and Listed == Thing and not Thing.HumanoidRootPart:FindFirstChild('InteractGui') then
				print('Found')
				AddInteract(Thing)
				
			end
		end
	end
	
end

SetUp()

I actually do not know if this is the script that is causing the hanging each time I press play a different line "hangs". (I cut a lot of the script due to too long)

1 answer

✓ Accepted answer

I can't see anything in your code there that would cause any sort of hanging code so it likely is elsewhere. But to answer your question, another way to cause an infinite loop that a lot of time produces no errors is calling a Module Script in a Module Script, that also calls the same Module Script back.

Module1

local module2 = require(game.ServerScriptService.Module2)

Module2

local module1 = require(game.ServerScriptService.Module1)

But on your quest to find places where your code might be creating infinite loops, just look for places that loop. And determine if there is any sort of case where they potentially could never end. For example if you have a recursive like function that never completed. It probably is something that isn't as obvious as while true do end

This discussion is preserved as it was. New answers, comments, and votes are closed.