How do i make a NPC behave this way?

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.

3 Likes

Nvm, i’ve found out how to do it.

something like this:

--Services--

local RunService = game:GetService("RunService")

--Variables--

local npc = workspace:FindFirstChild("NPC")
local npcRootPart = npc:FindFirstChild("HumanoidRootPart")
local player = game.Players.LocalPlayer
local playerCharacter = player.Character or player.CharacterAdded:Wait()

--Functions--

local function moveToPlayer()
		if playerCharacter and playerCharacter:FindFirstChild("HumanoidRootPart") then
			local playerPosition : Vector3 = playerCharacter.HumanoidRootPart.Position
			local npcRootPart = npc:FindFirstChild("HumanoidRootPart")

			if npcRootPart then
				local direction = (playerPosition - npcRootPart.Position).Unit
				local newPosition = npcRootPart.Position + direction * 0.4 
				npcRootPart.CFrame = CFrame.new(newPosition, playerPosition)
			end
		end
	task.wait() 
end

local function onTouch(other)
	if other:IsDescendantOf(playerCharacter) then
		local Hum = playerCharacter:FindFirstChild("Humanoid")
		Hum:ChangeState(Enum.HumanoidStateType.Dead)
	end
end

--Connections--

npcRootPart.Touched:Connect(onTouch)

RunService.Stepped:Connect(moveToPlayer)

i don’t know if it’s optimized tho, so i’ll may change it later.

2 Likes

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