What do you want to achieve?
I want to make the npc look at the nearest player and walk to the player, without any animations.
What is the issue?
What solutions have you tried so far?
I’ve tried searching all roblox tutorials on youtube but nothing really worked.
task.wait(3)
local Monster = script.Parent
local PlayerService = game:GetService("Players")
local Distance = 30 --
function UpdateMonsterPos()
while true do
for _, player in ipairs(PlayerService:GetPlayers()) do
local PlayerCharacter = player.Character
if PlayerCharacter then
local PlayerPosition = PlayerCharacter.PrimaryPart.Position
local MonsterPos = Monster.PrimaryPart.Position
local MonsterPrimaryPart = Monster.PrimaryPart
if (PlayerPosition - MonsterPos).Magnitude <= Distance then
local Direction = (PlayerPosition - MonsterPos).Unit
Monster:SetPrimaryPartCFrame(CFrame.new(MonsterPos + Direction))
local lookAtRotation = CFrame.new(MonsterPos, PlayerPosition)
Monster:SetPrimaryPartCFrame(lookAtRotation)
end
end
end
task.wait(0.5)
end
end
UpdateMonsterPos()
task.wait(3)
local Monster = script.Parent
local PlayerService = game:GetService("Players")
local Distance = 30 --
function UpdateMonsterPos()
while true do
for _, player in ipairs(PlayerService:GetPlayers()) do
local PlayerCharacter = player.Character
if PlayerCharacter then
local PlayerPosition = PlayerCharacter.PrimaryPart.Position
local MonsterPos = Monster.PrimaryPart.Position
local MonsterPrimaryPart = Monster.PrimaryPart
if (PlayerPosition - MonsterPos).Magnitude <= Distance then
local Direction = (PlayerPosition - MonsterPos).Unit
Monster:SetPrimaryPartCFrame(CFrame.new(MonsterPos + Direction)*CFrame.new(1,0,1))
local lookAtRotation = CFrame.new(MonsterPos, PlayerPosition)
Monster:SetPrimaryPartCFrame(lookAtRotation)
end
end
end
task.wait(0.5)
end
end
UpdateMonsterPos()
Multiplying a CFrame by CFrame.new(1,0,1) makes it so it doesn’t tilt up or down
You can get the direction of the player and add in on to the monsters position while multiplying by a speed multiplier in this part of the code:
local lookAtRotation = CFrame.new(MonsterPos, PlayerPosition)
Monster:SetPrimaryPartCFrame(lookAtRotation)
Here is how I’d go about it:
local dir = (PlayerPosition - MonsterPos).Unit -- Direction vector
local lookAtRotation = CFrame.new(MonsterPos + (dir * 1) ,PlayerPosition) -- instead of one you can do .1 etc etc, however you need to make sure the script updates fast enough for the movement
Monster:SetPrimaryPartCFrame(lookAtRotation)