So recently I have been trying to improve my AI for my game Bacon Escape: Bacon Escape [ALPHA] - Roblox
This is probably the most stressful thing I have done, because whenever I clone the bot into the game his Pathfinding script always breaks. Whenever he moves he stutters a lot and can’t target players correctly. I did try to ChangeState(10)
, but it didn’t work. This only occurs when he is cloned, when he is just in workspace he works perfectly fine. Why is this the case?
Extra Note: He only breaks if he is cloned in the specific spot too, I am also using SoundRegions. His paths always fail when he is spawned too.
Code
local myHuman = script.Parent:WaitForChild("Humanoid")
local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
local torso = script.Parent:WaitForChild("LowerTorso")
local pathArgs = {
["AgentRadius"] = 2,
["AgentHeight"] = 3
}
local hit = Instance.new("Animation")
hit.AnimationId = "rbxassetid://04751027547"
local hitAnim = myHuman:LoadAnimation(hit)
local laugh = script.Parent.Laugh
local jump = script.Parent.jump
local growl = script.Parent.StomachGrowl
local squish = script.Parent.Squish
function walkRandomly()
local xRand = math.random(-50,50)
local zRand = math.random(-50,50)
local goal = myRoot.Position + Vector3.new(xRand, 0, zRand)
local path = game:GetService("PathfindingService"):CreatePath(pathArgs)
path:ComputeAsync(myRoot.Position, goal)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for _, waypoints in ipairs(waypoints) do
if waypoints.Action == Enum.PathWaypointAction.Jump then
myHuman.Jump = true
end
myHuman:MoveTo(waypoints.Position)
local timeOut = myHuman.MoveToFinished:Wait(1)
if not timeOut then
print("Stuck")
getUnstuck()
end
end
else
print("Path failed")
wait(1)
walkRandomly()
end
end
function getUnstuck()
myHuman:Move(Vector3.new(math.random(-1,1),0,math.random(-1,1)))
myHuman.Jump = true
wait(1)
end
function findDist(torso)
return (myRoot.Position - torso.Position).Magnitude
end
function findTarget()
local dist = 150
local target = nil
for i,v in ipairs(workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
if human and torso and v.Name ~= script.Parent.Name and game.Players:GetPlayerFromCharacter(v).Team ~= game.Teams.FryCook then
if findDist(torso) < dist and human.Health > 0 then
target = torso
dist = findDist(torso)
end
end
end
return target
end
function checkSight(target)
local ray = Ray.new(myRoot.Position, (target.Position - myRoot.Position).Unit * 20)
local hit,position = workspace:FindPartOnRayWithIgnoreList(ray,{script.Parent})
if hit then
if hit:IsDescendantOf(target.Parent) then
return true
end
end
return false
end
function pathToTarget(target)
local path = game:GetService("PathfindingService"):CreatePath(pathArgs)
path:ComputeAsync(myRoot.Position,target.Position)
if path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
for i,v in ipairs(waypoints) do
if v.Action == Enum.PathWaypointAction.Jump then
myHuman.Jump = true
end
myHuman:MoveTo(v.Position)
spawn(function()
wait(0.3)
if myHuman.WalkToPoint.Y > myRoot.Position.Y then
myHuman.Jump = true
end
end)
local moveSucess = myHuman.MoveToFinished:Wait()
if not moveSucess then
getUnstuck()
break
end
if checkSight(target) and math.abs(math.abs(myRoot.Position.Y) - math.abs(target.Position.Y)) < 3 then
break
end
if (target.Position - waypoints[#waypoints].Position).Magnitude > 30 then
break
end
if i % 5 == 0 then
if findTarget() ~= target then
break
end
end
end
else
getUnstuck()
print("Path failed")
end
end
local attackCool = true
function attack(target)
if attackCool == true then
attackCool = false
local rand = math.random(5)
if rand == 1 then
squish:Play()
end
myHuman.JumpPower = 25 * (math.abs(math.abs(myRoot.Position.Y) - math.abs(target.Position.Y))) / 2 + 10
myHuman.Jump = true
myRoot.Velocity = myRoot.CFrame.LookVector * 50
torsoTouched = torso.Touched:Connect(function(obj)
if obj.Parent.Name ~= script.Parent.Name then
local human = obj.Parent:FindFirstChild("Humanoid")
if human then
if human.Health > 0 then
hitAnim:Play()
human:TakeDamage(20)
if human.Health < 1 then
laugh:Play()
end
torsoTouched:Disconnect()
end
end
end
end)
spawn(function() wait(1) torsoTouched:Disconnect() end)
spawn(function() wait(0.1) myHuman.JumpPower = 50 wait(0.9) attackCool = true end)
end
end
myHuman.Jumping:Connect(function()
jump:Play()
end)
torso.Touched:Connect(function(obj)
local human = obj.Parent:FindFirstChild("Humanoid")
if human then
if human.Health < 1 then
myHuman.Jump = true
end
elseif obj.Position.Y >= myRoot.Position.Y then
myHuman.Jump = true
end
end)
function main()
myHuman:ChangeState(10)
local target = findTarget()
if target then
if checkSight(target) and math.abs(math.abs(myRoot.Position.Y) - math.abs(target.Position.Y)) < 3 then
myHuman:MoveTo(target.Position)
if findDist(target) < 15 then
attack(target)
end
else
pathToTarget(target)
end
else
if math.random(5) == 1 then
growl:Play()
end
walkRandomly()
end
end
while wait() do
if myHuman.Health < 1 then
break
end
main()
end
Thanks For Reading!