Mouse raycast randomly stopping

Im trying to make it so you can place NPCs on a certain part etc. Im using mouse raycast to choose the npc placement. However the raycast randomly stops. I narrowed down the issue to the cframe of the raycast randomly stopping however Im not sure why.

heres my script

local PhysicsService = game:GetService("PhysicsService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local events = ReplicatedStorage:WaitForChild("Events")
local npcs = ReplicatedStorage:WaitForChild("NPCS")
local SpawnNPCevent = events:WaitForChild("SpawnNPC")
local camera = workspace.CurrentCamera
local gui = script.Parent

local npcToSpawn = nil
local canPlace = false
local Rotation = 0

local function MouseRaycast(blacklist)
	local mousePosition = UserInputService:GetMouseLocation()
	local mouseRay = camera:ViewportPointToRay(mousePosition.X,mousePosition.Y)
	local raycastParems = RaycastParams.new()

	raycastParems.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParems.FilterDescendantsInstances = blacklist
	local raycastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, raycastParems)

	return raycastResult
end

local function RemoveNPCplaceholder()
	if npcToSpawn then
		npcToSpawn:Destroy()
		npcToSpawn = nil
		Rotation = 0
	end
end

local function AddNPCplaceholder(name)

	local NPCexists = npcs:FindFirstChild(name)
	if NPCexists then
		RemoveNPCplaceholder()
		npcToSpawn = NPCexists:Clone()
		npcToSpawn.Parent = workspace.Soldiers
		npcToSpawn.HumanoidRootPart.Rotate.Enabled = true
		npcToSpawn.HumanoidRootPart.Place.Enabled = true
		npcToSpawn.HumanoidRootPart.Rotate.Enabled = true
		npcToSpawn.HumanoidRootPart.Cancel.Enabled = true

		for i, object in ipairs(npcToSpawn:GetDescendants()) do
			if object:IsA("BasePart") then
				object.CollisionGroup = "NPC"
				object.Material = Enum.Material.ForceField
			end
		end
	end
end

local function ColourPlaceholder(color)
	for i, object in ipairs(npcToSpawn:GetDescendants()) do
		if object:IsA("BasePart") then
			object.Color = color
		end
	end
end

gui.Spawn.Activated:Connect(function()
	AddNPCplaceholder("BlueSoldier")
end)

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end

	if npcToSpawn then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			if canPlace then
				SpawnNPCevent:FireServer(npcToSpawn.Name, npcToSpawn.PrimaryPart.CFrame)
				RemoveNPCplaceholder()
			end
		elseif input.KeyCode == Enum.KeyCode.R then
			Rotation += 90
		elseif input.KeyCode == Enum.KeyCode.Z then
			RemoveNPCplaceholder()
		end
	end
end)

RunService.RenderStepped:Connect(function()
	if npcToSpawn then
		print("exists")
		local result = MouseRaycast({npcToSpawn})
		if result and result.Instance then
			print("instance")
			if result.Instance.Parent.Name == "NpcSpawnArea" then
				canPlace = true
				ColourPlaceholder(Color3.fromRGB(0, 255, 0))
				npcToSpawn.HumanoidRootPart.Place.Place.TextColor3 = Color3.fromRGB(0, 255, 0)
			else
				canPlace = false
				ColourPlaceholder(Color3.fromRGB(255, 0, 0))
				npcToSpawn.HumanoidRootPart.Place.Place.TextColor3 = Color3.fromRGB(255, 0, 0)
			end
			local x = result.Position.X
			local y = result.Position.Y + 2 + (npcToSpawn.PrimaryPart.Size.Y / 2)
			local z = result.Position.Z

			local cframe = CFrame.new(x,y,z) * CFrame.Angles(0,math.rad(Rotation),0)
			--print(cframe)
			npcToSpawn:SetPrimaryPartCFrame(cframe)
		end
	end
end)

narrowed down the issue to the fact the placeholder already exists but the condition is met false and the if statement in the renderstepped thinks it doesnt exist

What do you by “Randomly Stopping?”

What is happening?

This prints the location of my mouse every renderstepped. However it randomly stops printing. I havent noticed a pattern.

Perhaps one of the if statement conditions arent met but Im not sure as they should all be met accordingly

Edit: Ive narrowed down the problem to

This is odd because there is always an npc that has the same name as “name” in the npcs folder

Edit 2: narrowed down the issue to the fact the placeholder already exists but the condition is met false and the if statement in the renderstepped thinks it doesnt exist

the bug doesnt happen when i play. only in studio