NPC not rotating to the Monster npc

Hey so i made this script that makes an npc always look at the monster npc in workspace but so i had this script without tweenserivce that worked but when i added tweenserivce it stopped working anyone know why its not work BTW THERES NO ERRORS IN OUTPUT

local remoteEvent = game.ReplicatedStorage:WaitForChild("TowerEvent")

local function findNearestMonster(npc)
	local nearestMonster = nil
	local shortestDistance = math.huge
	for _, monster in ipairs(workspace:GetChildren()) do
		if monster:IsA("Model") and monster.Name == "Monster" and monster.PrimaryPart then
			local distance = (monster.PrimaryPart.Position - npc.PrimaryPart.Position).Magnitude
			if distance < shortestDistance then
				shortestDistance = distance
				nearestMonster = monster
			end
		end
	end
	return nearestMonster
end

local function makeNpcFaceNearestMonster(npc)
	local TweenService = game:GetService("TweenService")
	local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)

	while npc.Parent do
		local nearestMonster = findNearestMonster(npc)
		if nearestMonster then
			local npcPosition = npc.PrimaryPart.Position
			local monsterPosition = nearestMonster.PrimaryPart.Position
			local lookAtCFrame = CFrame.lookAt(npcPosition, monsterPosition)

			local goal = { CFrame = lookAtCFrame }
			local tween = TweenService:Create(npc.PrimaryPart, tweenInfo, goal)
			tween:Play()
		end
		wait(0.5)
	end
end

remoteEvent.OnServerEvent:Connect(function(player, npcCFrame)
	local npcModel = game.ReplicatedStorage:WaitForChild("NPCModel"):Clone()
	npcModel:SetPrimaryPartCFrame(npcCFrame)
	npcModel.Parent = workspace

	makeNpcFaceNearestMonster(npcModel)
end)

1 Like

I would recommend adding some prints to ensure the tweens are being played. There’s probably an issue somewhere else in your code that is resulting in the tween:Play() line not running.

1 Like

I already fixed that lol! sorry

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