I need some help with scripting an npc

Trying to make a NPC that will follow a part facing it similar to a goalkeeper in these two games.

I don’t want the NPC to come out of the goalpost/forward, I just want it to move left or right. I’m also trying to use CFrame since it has better physics.

Can you show videos of examples? Also, please share any ideas if you want to approach to your goal.

robloxapp-20230408-2143542.wmv (5.1 MB)

You can use math.clamp, or only change the X values to make it move left and right

Could you explain it to me a bit better?

for it to only move right and left, when making it move only change the X value (or Z depending on orientation of the pitch)
so to make it move 1 stud to the right, multiply cframe by CFrame.new(1,0,0)
and make the 1 negative to move it one stud to the left

and to make sure the goalie doesnt move out of the goal, you can clamp the variable with math.clamp

--[[quick example]]--
--lets say 0,0,0 is the middle of the pitch.
local ballCFrame = CFrame.new(5,0,-4)
-- the ball was kicked to the right and forward
local goalie = --reference to goalie : Model
goalie:PivotTo(CFrame.new(ballCFrame.X, 0,0))
--only move the goalie on X axis

--[[Complicated - Math.Clamp]]--
local ballCFrame = CFrame.new(5,0,-4)
local goalie = --reference to goalie : Model
local min = 3 -- so the goalie doesn't move out the goal
local max = 5 -- ^^
local x = math.clamp(ballCFrame.X, min, max)
-- if the X value is more then the goal, then the goalie will stay at the edge of the goal. If it is less than the goal, goalie stays at other edge.
goalie:PivotTo(CFrame.new(x, 0,0))

Obviously this would be wrapped in a loop so it moves the goalie constantly

GoalieFollow.rbxl (80.3 KB)

You can Use CFrame.lookAt to make the front Face of a Part face another Object.

Whenever I play the game, the goalie just teleports to a different place. So how could I make it so the goalie would stay at the place it was originally at.

What do you mean? Please show an example

Disable the script, it is causing the goalie to move to the position of the ball

Which part of the script I need more context.

In the script proprties, set disabled to true

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