I recently joined roblox devforum and am still a learning dev.
I am not very good at pathfinding. I recently tried making an underwater pathfinding script and it ended up being a complete disaster. The monster was going upside down for no reason, not tracking the player at all at times, jumping when the player jumped, accelerating backwards, jumping for no reason instead of swimming.
Here is the script:
– Variables –
local Swimminganim = script.Parent.Humanoid:LoadAnimation(script.Parent.Swimming)
Swimminganim.Looped = true
Swimminganim:Play()
local Pathfinding = game:GetService(“PathfindingService”)
local Players = game:GetService(“Players”)
local RunService = game:GetService(“RunService”)
local config = game.ServerScriptService.OceanConfig
local bitedebounce = false
local path = Pathfinding:CreatePath({
AgentHeight = 12;
AgentRadius = 14;
AgentCanJump = false;
Cost = {
Water = 100;
DangerZone = math.huge
}
})
local Character = script.Parent
local humanoid = Character:WaitForChild(“Humanoid”)
local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
local PositionToMove = nil
local function findTarget()
local maxDistance = 5000
local nearestTarget
for index, player in pairs(Players:GetPlayers()) do
if player.Character and game.Players:GetPlayerFromCharacter(player.Character) then
local target = player.Character
local distance = (Character.AttackLocation.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance then
nearestTarget = target
maxDistance = distance
end
if distance < 15 and bitedebounce == false then
bitedebounce = true
if target:FindFirstChild("HumanoidRootPart") then
target.HumanoidRootPart.CFrame.Position = Character.AttackLocation.CFrame.Position
else
target.Torso.CFrame.Position = Character.AttackLocation.CFrame.Position
end
nearestTarget.Humanoid:TakeDamage(35)
--local attackanim = Character.Humanoid:LoadAnimation(script.Parent.Attack)
--attackanim.Priority = Enum.AnimationPriority.Action2
--attackanim:Play()
local attacksound = script.Bite:Clone()
attacksound.Parent = Character
attacksound:Play()
task.delay(1,function()
bitedebounce = false
end)
end
end
end
return nearestTarget
end
local function followPath(destination)
local success, errormessage = pcall(function()
path:ComputeAsync(Character.AttackLocation.Position, destination)
end)
if success and path.Status == Enum.PathStatus.Success then
waypoints = path:GetWaypoints()
blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
if blockedWaypointIndex >= nextWaypointIndex then
blockedConnection:Disconnect()
followPath(destination)
end
end)
if not reachedConnection then
reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
if reached and nextWaypointIndex < #waypoints then
nextWaypointIndex += 1
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
reachedConnection:Disconnect()
blockedConnection:Disconnect()
end
end)
end
nextWaypointIndex = 2
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
end
end
while wait() do
local target = findTarget()
if target and game.Players:GetPlayerFromCharacter(target) then
followPath(target.HumanoidRootPart.Position)
end
end
The attacklocation is a part in the monster’s mouth. Really need help right now.
Thank You!