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)