Hi there, im making a Pizza Tower inspired game currently, and one of the things i want to do is make a NPC which acts similarly to one of the characters of this game im taking inspiration, Pizzaface, which in case that you dont know what he does, he walks to the player, ignoring any walls, and making the player lose when in contact.
I’ve tried to implement this to Studio, making a raycast that starts on the NPC and goes to the player’s position, and making it move to the raycast position, but the NPC only moves left or right, which is not what i want it to do.
Here’s the script that i made:
--Variables--
local NPC = workspace:WaitForChild("NPC")
local Root = NPC:WaitForChild("HumanoidRootPart")
local Huma = NPC:WaitForChild("Humanoid")
local PlrPositionUpdater = game.ReplicatedStorage:WaitForChild("PlrPositionUpdater")
local StartingRoom = workspace:WaitForChild("StartingRoom")
local GeneratedRooms = workspace:WaitForChild("GeneratedRooms")
local debounce = false
local debounceTime = 0.1
--Functions--
local function NPCmover(plrPos : Vector3?)
if not plrPos then return end
local direction = (plrPos - Root.Position).Unit * 100
local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Exclude
Params.FilterDescendantsInstances = {NPC, StartingRoom, GeneratedRooms}
local rayResult = workspace:Raycast(Root.Position, direction, Params)
if rayResult then
Huma:MoveTo(rayResult.Position)
Huma.MoveToFinished:Wait()
else
print(nil)
end
end
local function Mover(plr, plrPos)
if not debounce then
debounce = true
NPCmover(plrPos)
task.wait(debounceTime)
debounce = false
end
end
--Connections--
PlrPositionUpdater.OnServerEvent:Connect(Mover)
Sorry if the code is messy, this is my first time sharing any code i’ve made.