Guys How Can I Make A Water NPC That Goes Up And Down In Water Terrain, i already tryed make a “Fly” System to Simulate UnderWater, But it Don’t Move…
Script:
local Fish = script.Parent
local Humanoid = Fish.Humanoid
local Primary = Fish.PrimaryPart
local SimplePath = require(game.ServerStorage.SimplePath, {AgentRadius = 10, AgentHeight = 6})
local Path = SimplePath.new(Fish)
local PositionToMove = nil
local BodyPosition = Instance.new("BodyPosition", Primary)
BodyPosition.MaxForce = Vector3.new(0, math.huge, 0)
BodyPosition.P = 10
function FindTarget()
local MinimalDistance = 150
local Target = nil
for i, Player in game.Players:GetPlayers() do
if Player.Team ~= game.Teams.Entity then
if Player.Character then
if Player.Character.PrimaryPart then
local Distance = (Primary.Position - Player.Character.PrimaryPart.Position).Magnitude
if Distance < MinimalDistance then
Target = Player.Character
MinimalDistance = Distance
end
end
end
end
end
return Target
end
local function MoveRandom()
if PositionToMove ~= nil then
BodyPosition.Position = PositionToMove
Humanoid.WalkToPoint = PositionToMove
else
PositionToMove = Vector3.new(Primary.Position.X + math.random(-50, 50), math.clamp(Primary.Position.Y + math.random(-50, 50), 260.983, 639.983), Primary.Position.Z + math.random(-50, 50))
BodyPosition.Position = PositionToMove
Humanoid.WalkToPoint = PositionToMove
end
Humanoid.MoveToFinished:Connect(function()
PositionToMove = nil
end)
end
local function MoveToPlayer(target)
BodyPosition.Position = target.PrimaryPart.Position
Humanoid.WalkToPoint = target.PrimaryPart.Position
end
while wait(0.1) do
local Target = FindTarget()
if Target then
MoveToPlayer(Target)
else
MoveRandom()
end
end
Your script is using both Humanoid.WalkToPoint and BodyPosition, which may be conflicting, causing the fish not to move properly. In underwater movement, Humanoid movement is not ideal, so you should rely more on BodyPosition and possibly BodyGyro for smooth navigation.
Fixing the Movement Issues:
Remove Humanoid.WalkToPoint – Humanoids are meant for walking on land, not floating in water.
Increase BodyPosition.P – A value of 10 is too weak to move the fish effectively. Try increasing it to at least 5000 or more.
Add BodyGyro for Stability – This helps keep the fish upright.
Use VectorForce for Smooth Movement – Instead of relying on BodyPosition alone, you can apply force-based movement.
Edited Script:
local Fish = script.Parent
local Primary = Fish.PrimaryPart
local SimplePath = require(game.ServerStorage.SimplePath, {AgentRadius = 10, AgentHeight = 6})
local Path = SimplePath.new(Fish)
local PositionToMove = nil
-- Body movers for smooth underwater movement
local BodyPosition = Instance.new("BodyPosition", Primary)
BodyPosition.MaxForce = Vector3.new(1e6, 1e6, 1e6) -- High force for better movement
BodyPosition.P = 5000
local BodyGyro = Instance.new("BodyGyro", Primary)
BodyGyro.MaxTorque = Vector3.new(1e6, 1e6, 1e6) -- Stabilizes rotation
BodyGyro.P = 5000
function FindTarget()
local MinimalDistance = 150
local Target = nil
for _, Player in game.Players:GetPlayers() do
if Player.Team ~= game.Teams.Entity then
if Player.Character and Player.Character.PrimaryPart then
local Distance = (Primary.Position - Player.Character.PrimaryPart.Position).Magnitude
if Distance < MinimalDistance then
Target = Player.Character
MinimalDistance = Distance
end
end
end
end
return Target
end
local function MoveRandom()
PositionToMove = Vector3.new(
Primary.Position.X + math.random(-50, 50),
math.clamp(Primary.Position.Y + math.random(-50, 50), 260.983, 639.983),
Primary.Position.Z + math.random(-50, 50)
)
BodyPosition.Position = PositionToMove
end
local function MoveToPlayer(target)
BodyPosition.Position = target.PrimaryPart.Position
end
while task.wait(1) do
local Target = FindTarget()
if Target then
MoveToPlayer(Target)
else
MoveRandom()
end
end
Key Fixes:
Removed Humanoid.WalkToPoint – It doesn’t work well underwater. Increased BodyPosition.P – Stronger force for movement. Added BodyGyro – Keeps the fish upright. Removed unnecessary MoveToFinished event – The BodyPosition handles movement smoothly.
This will allow the NPC to move up and down properly in water and chase players when they get close.