Game freezes for 20~ seconds when entity despawns

I am trying to make a scary monster for my friend’s game, here is the code, and video footage:

Utility = require(game.ReplicatedStorage.Utility)
local RunService = game:GetService("RunService")

local currentRoom, currentRoomModel = Utility.GetCurrentRoom()
local lastRoom, lastRoomModel = Utility.GetLastRoom()
local latestRoom, latestRoomModel = Utility.GetLastRoom()

function FindPlayers(part)
	local overlapParams = OverlapParams.new()
	overlapParams.FilterType = Enum.RaycastFilterType.Include
	overlapParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
	local nearcharacters = workspace:GetPartBoundsInRadius(part.Parent:WaitForChild("Torso").Position, 100, overlapParams)
	local collisions = workspace:GetPartBoundsInRadius(part.Parent:WaitForChild("Torso").Position, 9, overlapParams)

	for index, obj in ipairs(nearcharacters) do
		if obj.Name == "HumanoidRootPart" then
			if obj.Parent == game.Players.LocalPlayer.Character then
				local rayDirection = obj.Position - part.Parent:WaitForChild("Torso").Position
				local result = workspace:Raycast(part.Parent:WaitForChild("Torso").Position, rayDirection)
				if result and result.Instance then
					local hit = result.Instance
					if hit == obj or hit:FindFirstAncestor(obj.Parent.Name) then
						if not obj.Parent.Humanoid.Sit and obj.Parent.Humanoid.Health > 0 and obj.Parent.Humanoid.Health < 10000 then
							obj.Parent:WaitForChild("Humanoid").Health = 0
						end
					end
				end
			end
		end
	end
	for index, obj in ipairs(collisions) do
		if obj.Name == "HumanoidRootPart" then
			if obj.Parent == game.Players.LocalPlayer.Character then
				local rayDirection = obj.Position - part.Parent:WaitForChild("Torso").Position
				local result = workspace:Raycast(part.Parent:WaitForChild("Torso").Position, rayDirection)
				if result and result.Instance then
					local hit = result.Instance
					if hit == obj or hit:FindFirstAncestor(obj.Parent.Name) then
						if not obj.Parent.Humanoid.Sit and obj.Parent.Humanoid.Health > 0 and obj.Parent.Humanoid.Health < 10000 then
							obj.Parent:WaitForChild("Humanoid").Health = 0
						end
					end
				end
			end
		end
	end
end

function LerpTo(part, target, spawning, isWaypoint)
	local alpha = 0
	local speed = 200

	local targetPosition = target.Position + Vector3.new(0,3.5,0)

	if spawning then
		speed *= 1
	end	

	local distance = (part.Parent:WaitForChild("Torso").Position - targetPosition).Magnitude
	local relativeSpeed = distance / speed
	local startCFrame = part.Parent:WaitForChild("Torso").CFrame
	local loop = nil
	local reachedTarget = Instance.new("BindableEvent")

	loop = RunService.Heartbeat:Connect(function(delta)
		local lastRoom, lastRoomModel = Utility.GetLastRoom()
		local latestRoom, latestRoomModel = Utility.GetLatestRoom()
		if spawning then
			target = lastRoomModel.Entrance
		end

		local goalCFrame
		if isWaypoint then
			goalCFrame = startCFrame:Lerp(target.CFrame + Vector3.new(0,3.5,0), alpha)
		else
			goalCFrame = startCFrame:Lerp(target.CFrame, alpha)
		end
		part:PivotTo(goalCFrame)
		if spawning then
			part:PivotTo(CFrame.new(target.Position.X, goalCFrame.Y, target.Position.Z))
		end
		alpha += delta / relativeSpeed
		if alpha >= 1 then
			loop:Disconnect()
			reachedTarget:Fire()
		end
	end)

	reachedTarget.Event:Wait()
end

function Navigate(part, generatedRooms, spawning)
	local errorCount = 0
	local spawning = spawning
	local room = nil
	local currentWaypoint = nil
	local currentWaypointFolder = nil
	local success, result = pcall(function()
		local lastRoom, lastRoomModel = Utility.GetLastRoom()
		local latestRoom, latestRoomModel = Utility.GetLatestRoom()
		if room == nil then
			room = lastRoom
		end
		for i=room, 10000, 1 do
			room = i
			lastRoom, lastRoomModel = Utility.GetLastRoom()
			latestRoom, latestRoomModel = Utility.GetLatestRoom()
			if spawning then
				LerpTo(part, lastRoomModel.Exit, true, false)
				spawning = false
			end
			local curRoomModel = workspace.GeneratedRooms:FindFirstChild(i)
			LerpTo(part, curRoomModel.Entrance, false, false)
			local waypointFolders = {}
			for _, v in pairs(curRoomModel:GetChildren()) do
				if v.Name == "Waypoints" then
					table.insert(waypointFolders, v)
				end
			end
			if #waypointFolders > 0 then
				local waypoints = currentWaypointFolder or Utility.GetRandomFromTable(waypointFolders, 1)
				if currentWaypoint then
					for w=currentWaypoint, #waypoints:GetChildren(), 1 do
						LerpTo(part, waypoints[w], false, true)
						currentWaypoint = w
					end
				else
					for w=1, #waypoints:GetChildren(), 1 do
						LerpTo(part, waypoints[w], false, true)
						currentWaypoint = w
					end
				end
			else
				currentWaypoint = nil
				currentWaypointFolder = nil
			end
			LerpTo(part, curRoomModel.Exit, false, false)

			currentWaypoint = nil
			currentWaypointFolder = nil

			if tonumber(curRoomModel.Name) == latestRoom then
				break
			end
		end
	end)

	if not success then
		if errorCount < 10 then
			errorCount += 1
			print(result)
			Navigate(part, generatedRooms)
		else
			warn(result)
			script.Parent:Destroy()
		end
	end
end

function NavigateBackwards(part, generatedRooms, spawning)
	local errorCount = 0
	local spawning = spawning
	local room = nil
	local currentWaypoint = nil
	local currentWaypointFolder = nil
	local success, result = pcall(function()
		local lastRoom, lastRoomModel = Utility.GetLastRoom()
		local latestRoom, latestRoomModel = Utility.GetLatestRoom()
		if spawning then
			LerpTo(part, latestRoomModel.Exit, true, false)
			spawning = false
		end
		lastRoom, lastRoomModel = Utility.GetLastRoom()
		latestRoom, latestRoomModel = Utility.GetLatestRoom()
		if room == nil then
			room = latestRoom
		end

		for i=room, 0, -1 do
			room=i
			lastRoom, lastRoomModel = Utility.GetLastRoom()
			latestRoom, latestRoomModel = Utility.GetLatestRoom()
			local curRoomModel = workspace.GeneratedRooms:FindFirstChild(i)
			LerpTo(part, curRoomModel.Exit, false, false)
			local waypointFolders = {}
			for _, v in pairs(workspace.GeneratedRooms:FindFirstChild(i):GetChildren()) do
				if v.Name == "Waypoints" then
					table.insert(waypointFolders, v)
				end
			end
			if #waypointFolders > 0 then
				local waypoints = currentWaypointFolder or Utility.GetRandomFromTable(waypointFolders, 1)
				currentWaypointFolder = waypoints
				if currentWaypoint ~= nil then
					for w=currentWaypoint, 1, -1 do
						LerpTo(part, waypoints[w], false, true)
						currentWaypoint = w
					end
				else
					for w=#waypoints:GetChildren(), 1, -1 do
						LerpTo(part.Parent, waypoints[w], false, true)
						currentWaypoint = w
					end
				end
			else
				currentWaypoint = nil
				currentWaypointFolder = nil
			end
			LerpTo(part, curRoomModel.Entrance, false, false)

			currentWaypoint = nil
			currentWaypointFolder = nil

			if tonumber(curRoomModel.Name) == lastRoom then
				break
			end
		end
	end)

	if not success then
		if errorCount < 10 then
			errorCount += 1
			NavigateBackwards(part.Parent:WaitForChild("Torso"), generatedRooms, false)
		else
			warn(result)
			script.Parent:Destroy()
		end
	end
end
script.Parent:PivotTo(lastRoomModel.Entrance.CFrame + Vector3.new(0,-3000,0))
print("Blitz spawned")
script.Parent.hunt.Value = true
task.wait(1)
Navigate(script.Parent.Torso, workspace.GeneratedRooms, true)


despawnLength = 2
--------
entity = script.Parent
entity.hunt.Value = false
wait(despawnLength)
script.Parent:Destroy()

2 Likes

Well, when the pause ends, it says "Workspace.ActiveEntities.Ripper.Spawn59: attempt to index nil with WaitForChild (x1118). SO, probably, the reason that this might be happening is because of that glitch.

5 Likes

I don’t understand what is wrong with it, did the part delete?

3 Likes

Okay, I stopped the lag, but he doesn’t show up.

Utility = require(game.ReplicatedStorage.Utility)
local RunService = game:GetService("RunService")

local currentRoom, currentRoomModel = Utility.GetCurrentRoom()
local lastRoom, lastRoomModel = Utility.GetLastRoom()
local latestRoom, latestRoomModel = Utility.GetLastRoom()

function FindPlayers(part)
	local overlapParams = OverlapParams.new()
	overlapParams.FilterType = Enum.RaycastFilterType.Include
	overlapParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
	local nearcharacters = workspace:GetPartBoundsInRadius(part.Parent:WaitForChild("Torso").Position, 100, overlapParams)
	local collisions = workspace:GetPartBoundsInRadius(part.Parent:WaitForChild("Torso").Position, 9, overlapParams)

	for index, obj in ipairs(nearcharacters) do
		if obj.Name == "HumanoidRootPart" then
			if obj.Parent == game.Players.LocalPlayer.Character then
				local rayDirection = obj.Position - part.Parent:WaitForChild("Torso").Position
				local result = workspace:Raycast(part.Parent:WaitForChild("Torso").Position, rayDirection)
				if result and result.Instance then
					local hit = result.Instance
					if hit == obj or hit:FindFirstAncestor(obj.Parent.Name) then
						if not obj.Parent.Humanoid.Sit and obj.Parent.Humanoid.Health > 0 and obj.Parent.Humanoid.Health < 10000 then
							obj.Parent:WaitForChild("Humanoid").Health = 0
						end
					end
				end
			end
		end
	end
	for index, obj in ipairs(collisions) do
		if obj.Name == "HumanoidRootPart" then
			if obj.Parent == game.Players.LocalPlayer.Character then
				local rayDirection = obj.Position - part.Parent:WaitForChild("Torso").Position
				local result = workspace:Raycast(part.Parent:WaitForChild("Torso").Position, rayDirection)
				if result and result.Instance then
					local hit = result.Instance
					if hit == obj or hit:FindFirstAncestor(obj.Parent.Name) then
						if not obj.Parent.Humanoid.Sit and obj.Parent.Humanoid.Health > 0 and obj.Parent.Humanoid.Health < 10000 then
							obj.Parent:WaitForChild("Humanoid").Health = 0
						end
					end
				end
			end
		end
	end
end

function LerpTo(part, target, spawning, isWaypoint)
	if part and part.Parent then
		local alpha = 0
		local speed = 200

		local targetPosition = target.Position + Vector3.new(0,3.5,0)

		if spawning then
			speed *= 1
		end	

		local distance = (part.Parent:WaitForChild("Torso").Position - targetPosition).Magnitude
		local relativeSpeed = distance / speed
		local startCFrame = part.Parent:WaitForChild("Torso").CFrame
		local loop = nil
		local reachedTarget = Instance.new("BindableEvent")

		loop = RunService.Heartbeat:Connect(function(delta)
			local lastRoom, lastRoomModel = Utility.GetLastRoom()
			local latestRoom, latestRoomModel = Utility.GetLatestRoom()
			if spawning then
				target = lastRoomModel.Entrance
			end

			local goalCFrame
			if isWaypoint then
				goalCFrame = startCFrame:Lerp(target.CFrame + Vector3.new(0,3.5,0), alpha)
			else
				goalCFrame = startCFrame:Lerp(target.CFrame, alpha)
			end
			part:PivotTo(goalCFrame)
			if spawning then
				part:PivotTo(CFrame.new(target.Position.X, goalCFrame.Y, target.Position.Z))
			end
			alpha += delta / relativeSpeed
			if alpha >= 1 then
				loop:Disconnect()
				reachedTarget:Fire()
			end
		end)

		reachedTarget.Event:Wait()
	end
end

function Navigate(part, generatedRooms, spawning)
	local errorCount = 0
	local spawning = spawning
	local room = nil
	local currentWaypoint = nil
	local currentWaypointFolder = nil
	local success, result = pcall(function()
		local lastRoom, lastRoomModel = Utility.GetLastRoom()
		local latestRoom, latestRoomModel = Utility.GetLatestRoom()
		if room == nil then
			room = lastRoom
		end
		for i=room, 10000, 1 do
			room = i
			lastRoom, lastRoomModel = Utility.GetLastRoom()
			latestRoom, latestRoomModel = Utility.GetLatestRoom()
			if spawning then
				LerpTo(part, lastRoomModel.Exit, true, false)
				spawning = false
			end
			local curRoomModel = workspace.GeneratedRooms:FindFirstChild(i)
			LerpTo(part, curRoomModel.Entrance, false, false)
			local waypointFolders = {}
			for _, v in pairs(curRoomModel:GetChildren()) do
				if v.Name == "Waypoints" then
					table.insert(waypointFolders, v)
				end
			end
			if #waypointFolders > 0 then
				local waypoints = currentWaypointFolder or Utility.GetRandomFromTable(waypointFolders, 1)
				if currentWaypoint then
					for w=currentWaypoint, #waypoints:GetChildren(), 1 do
						LerpTo(part, waypoints[w], false, true)
						currentWaypoint = w
					end
				else
					for w=1, #waypoints:GetChildren(), 1 do
						LerpTo(part, waypoints[w], false, true)
						currentWaypoint = w
					end
				end
			else
				currentWaypoint = nil
				currentWaypointFolder = nil
			end
			LerpTo(part, curRoomModel.Exit, false, false)

			currentWaypoint = nil
			currentWaypointFolder = nil

			if tonumber(curRoomModel.Name) == latestRoom then
				break
			end
		end
	end)

	if not success then
		if errorCount < 10 then
			errorCount += 1
			print(result)
			Navigate(part, generatedRooms)
		else
			warn(result)
			script.Parent:Destroy()
		end
	end
end

function NavigateBackwards(part, generatedRooms, spawning)
	local errorCount = 0
	local spawning = spawning
	local room = nil
	local currentWaypoint = nil
	local currentWaypointFolder = nil
	local success, result = pcall(function()
		local lastRoom, lastRoomModel = Utility.GetLastRoom()
		local latestRoom, latestRoomModel = Utility.GetLatestRoom()
		if spawning then
			LerpTo(part, latestRoomModel.Exit, true, false)
			spawning = false
		end
		lastRoom, lastRoomModel = Utility.GetLastRoom()
		latestRoom, latestRoomModel = Utility.GetLatestRoom()
		if room == nil then
			room = latestRoom
		end

		for i=room, 0, -1 do
			room=i
			lastRoom, lastRoomModel = Utility.GetLastRoom()
			latestRoom, latestRoomModel = Utility.GetLatestRoom()
			local curRoomModel = workspace.GeneratedRooms:FindFirstChild(i)
			LerpTo(part, curRoomModel.Exit, false, false)
			local waypointFolders = {}
			for _, v in pairs(workspace.GeneratedRooms:FindFirstChild(i):GetChildren()) do
				if v.Name == "Waypoints" then
					table.insert(waypointFolders, v)
				end
			end
			if #waypointFolders > 0 then
				local waypoints = currentWaypointFolder or Utility.GetRandomFromTable(waypointFolders, 1)
				currentWaypointFolder = waypoints
				if currentWaypoint ~= nil then
					for w=currentWaypoint, 1, -1 do
						LerpTo(part, waypoints[w], false, true)
						currentWaypoint = w
					end
				else
					for w=#waypoints:GetChildren(), 1, -1 do
						LerpTo(part.Parent, waypoints[w], false, true)
						currentWaypoint = w
					end
				end
			else
				currentWaypoint = nil
				currentWaypointFolder = nil
			end
			LerpTo(part, curRoomModel.Entrance, false, false)

			currentWaypoint = nil
			currentWaypointFolder = nil

			if tonumber(curRoomModel.Name) == lastRoom then
				break
			end
		end
	end)

	if not success then
		if errorCount < 10 then
			errorCount += 1
			NavigateBackwards(part.Parent:WaitForChild("Torso"), generatedRooms, false)
		else
			warn(result)
			script.Parent:Destroy()
		end
	end
end
script.Parent:PivotTo(lastRoomModel.Entrance.CFrame + Vector3.new(0,-3000,0))
print("Blitz spawned")
script.Parent.hunt.Value = true
task.wait(1)
Navigate(script.Parent.Torso, workspace.GeneratedRooms, true)


despawnLength = 2
--------
entity = script.Parent
entity.hunt.Value = false
wait(despawnLength)
script.Parent:Destroy()

How do I make him show up?

3 Likes

Honestly, i’m just skimming the code. This is pretty hard.

4 Likes

There is multiple “script.Parent:Destroy()”. Why is the entity being deleted?

3 Likes

To despawn. I will give you test permissions so you can help me, ok? nvm join me

Sure thing! Although i am not too good at coding but ill try.

join me rq, i cant give tester cause i dont own group

Wait, so do I like, join the group or

1 Like

just join me 30CHARLIMITBRUHROBLOX

1 Like

wait, i gotta afk for a few. Maybe try looking for others on the dev forum. Gotta do a few things

3 Likes

Nvm for some reason he now appears???

2 Likes

I am back! Sorry for the long wait. Wait, so, if he appears… Is that the problem solved?

2 Likes

uhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
what now

3 Likes

Idk, it doesn’t work in studio

1 Like

It doesnt work anymore? charsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss

i had to say charsssss cause of the character limit

1 Like

Only in studio, ingame it works fine

bruh charrrrrrrrrrrrrrrrrrrrrs

2 Likes