Cannot reproduce in game glitch in studio and dev console doesnt give me useful information

Im working on a game that uses an autolocking system that makes your character face the closest enemy, it works absolutely flawlessly in studio. For example

code works as intended, closest enemy is highlighted and player model pivots to face it. However when you play the game, it works as intended for the first 2 or 3 rooms but then just stops working out of nowhere and returns this.

heres the code

local character = game.Players.LocalPlayer.Character
local humanoid = game.Players.LocalPlayer.Character.Humanoid
local tweenService = game:GetService("TweenService")

local function findClosestPart(group, position)
	local closestPart, closestPartMagnitude

	local tmpMagnitude -- to store our calculation 
	for i, v in pairs(group:GetChildren()) do
		if closestPart then -- we have a part)
			tmpMagnitude = (position - v.HumanoidRootPart.Position).magnitude

			-- check the next part
			if tmpMagnitude < closestPartMagnitude then
				closestPart = v
				closestPartMagnitude = tmpMagnitude 
			end
		else
			-- our first part
			closestPart = v
			closestPartMagnitude = (position - v.HumanoidRootPart.Position).magnitude
		end
	end
	
	return closestPart
end

humanoid.AutoRotate = false

while true do
	closestPart = findClosestPart(workspace.Enemies, script.Parent.HumanoidRootPart.Position)
	local enemyHighlight = game.ReplicatedStorage.Misc.EnemyHighlight:Clone()
	local targetUi = game.ReplicatedStorage.Misc.TargetUI:Clone()
	targetUi.Parent = closestPart
	enemyHighlight.Parent = closestPart
	wait()
	enemyHighlight:Destroy()
	targetUi:Destroy()
	if closestPart:FindFirstChild("HumanoidRootPart") then
		local position = Vector3.new(closestPart.HumanoidRootPart.position.X, character.Head.position.Y, closestPart.HumanoidRootPart.position.Z)

		if (closestPart.HumanoidRootPart.position - character.HumanoidRootPart.position).magnitude <= 30 then
			humanoid.AutoRotate = false
			local position = Vector3.new(closestPart.HumanoidRootPart.position.X, character.Head.position.Y, closestPart.HumanoidRootPart.position.Z)
			character:PivotTo(CFrame.new(character.PrimaryPart.Position, position))
		else
			humanoid.AutoRotate = true
		end
	end
end

OBS is extremely laggy on my laptop right now since i played around with the settings, heres the game if you want to see the bug in action for yourself

I would either:

  • Check if the PrimaryPart exists, and if not, continue-ing
  • Make sure each enemy model has a HumanoidRootPart and/or a PrimaryPart

Hope this helps!

I don’t know if this has anything to do with it, but try changing this:

to this:

local player = game.Players.LocalPlayer
local character = player.Character or player:CharacterAdded:Wait()
local humanoid = character.Humanoid

This way the game will have time to load the character and the Humanoid.

how would i go about doing that? when i try using WaitForChild() it returns infinate yield and just breaks again

didnt work sadly, its the HumanoidRootPart of the enemy that doesnt load properly not the players

In the explorer, do your enemies have a HumanoidRootPart?

And to continue a for loop is like skipping an item, and it would look like this:

for i, v in pairs(group:GetChildren()) do
    if not v.PrimaryPart then continue end -- Skips only this item of the list, and continues searching.
    -- rest of the code
end

Heres the solution, i fixed it by inserting this line of code between line 13 and 14

			local descendantsComplete = v:WaitForChild('HumanoidRootPart')
			while not descendantsComplete do
				runService.Heartbeat:Wait()
			end

complete fixed code:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character.Humanoid
local humanoid = game.Players.LocalPlayer.Character.Humanoid
local tweenService = game:GetService("TweenService")
local runService = game:GetService("RunService")

local function findClosestPart(group, position)
	local closestPart, closestPartMagnitude

	local tmpMagnitude -- to store our calculation 
	for i, v in pairs(group:GetChildren()) do
		if closestPart then -- we have a part)
			
			local descendantsComplete = v:WaitForChild('HumanoidRootPart')
			while not descendantsComplete do
				runService.Heartbeat:Wait()
			end
			
			tmpMagnitude = (position - v.HumanoidRootPart.Position).magnitude
		

			-- check the next part
			if tmpMagnitude < closestPartMagnitude then
				closestPart = v
				closestPartMagnitude = tmpMagnitude 
			end
		else
			-- our first part
			closestPart = v
			closestPartMagnitude = (position - v.HumanoidRootPart.Position).magnitude
		end
	end
	
	return closestPart
end

humanoid.AutoRotate = false

while true do
	closestPart = findClosestPart(workspace.Enemies, script.Parent.HumanoidRootPart.Position)
	local enemyHighlight = game.ReplicatedStorage.Misc.EnemyHighlight:Clone()
	local targetUi = game.ReplicatedStorage.Misc.TargetUI:Clone()
	targetUi.Parent = closestPart
	enemyHighlight.Parent = closestPart
	wait()
	enemyHighlight:Destroy()
	targetUi:Destroy()
	if closestPart:FindFirstChild("HumanoidRootPart") then
		local position = Vector3.new(closestPart.HumanoidRootPart.position.X, character.Head.position.Y, closestPart.HumanoidRootPart.position.Z)

		if (closestPart.HumanoidRootPart.position - character.HumanoidRootPart.position).magnitude <= 30 then
			humanoid.AutoRotate = false
			local position = Vector3.new(closestPart.HumanoidRootPart.position.X, character.Head.position.Y, closestPart.HumanoidRootPart.position.Z)
			character:PivotTo(CFrame.new(character.PrimaryPart.Position, position))
		else
			humanoid.AutoRotate = true
		end
	end
end


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