What I been trying to do is write a script Ai for my Martian tripod.
My Code Is messy and rushed. Would anyone like to rewrite it or send me feed back on how I could improve.
I did look up how to do AI from Youtube and the Dev fourm and put them all together to make this mess of a code.
So How I want it to work is that I first Follows way point blocks in a folder. Also For the raycasting to work with both eyes. So Both Eyes have to see it before interrupts its patrol script to follow the closest player within its sight and using the pathfinding service to make a possible path were the player is if the player is out of view to fast. And It will "Stalk the area the player was last seen for 3 mins before going back to its patrol script around the map.
Here is the Model: https://www.roblox.com/library/6052902084/Tripod-Ai
local humanoid = tripod.Humanoid
--Latency Fix
for i,v in ipairs(script.Parent:GetDescendants()) do
if v:IsA("BasePart") then
if v:CanSetNetworkOwnership() == true then
v:SetNetworkOwner(nil)
end
end
end
--Main Body Parts
local torso = script.Parent.Torso
local head = script.Parent.Head
local Reye = script.Parent.REye
local Leye = script.Parent.LEye
local myRoot = script.Parent.HumanoidRootPart
local neck = script.Parent.Torso.Neck
-- basket for player
local myTeam = script.Parent.Team
local cage = script.Parent.basket
-- tentacles
local Rtentacle = script.Parent.RTentacle
local Ltentacle = script.Parent.LTentacle]
-- sounds
local sight = 200
local detection = 3000
local failedPaths = 0
local touchedEvents = {}
local pathArgs = {
["AgentRadius"] = 140,
["AgentHeight"] = 100,
["AgentCanJump"] = false
}
local gunCool = true
local pathBlocked = false
function checkForObstacles(direction)
for i = -13, 13, 0.5 do
local ray
if direction == "front" then
ray = Ray.new((torso.CFrame * CFrame.new(i,1,-16)).Position, torso.CFrame.LookVector * 5)
elseif direction == "back" then
ray = Ray.new((torso.CFrame * CFrame.new(i,0,13)).Position, -torso.CFrame.LookVector * 5)
elseif direction == "right" then
ray = Ray.new((torso.CFrame * CFrame.new(13,-0.5,i)).Position, torso.CFrame.RightVector * 5)
elseif direction == "left" then
ray = Ray.new((torso.CFrame * CFrame.new(-13,-0.5,i)).Position, -torso.CFrame.RightVector * 5)
else
print("Invalid direction suppled")
ray = Ray.new(Vector3.new(0,0,0),Vector3.new(0,0,0))
end
local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray,{script.Parent})
if hit then
if hit:IsGrounded() or hit.Anchored then
return true
end
end
end
return false
end
--Collision detection logic
for _,v in ipairs(script.Parent:GetDescendants()) do
if v:IsA("BasePart") and v.Name ~= "RFoot" and v.Name ~= "LFoot" and v.Name ~= "BFoot" then
local db = true
local touch = v.Touched:Connect(function(obj)
if db == true then
db = false
if obj.Parent and not obj:IsDescendantOf(script.Parent) then
local human = obj.Parent:FindFirstChild("Humanoid")
if not human and obj.Position.Y >= torso.Position.Y - 0.5 then
--Break small objects that are in the way
if obj:IsGrounded() and obj:GetMass() < 100 and not obj.Anchored then
obj:BreakJoints()
elseif obj:IsGrounded() or obj.Anchored then
if checkForObstacles("front") then
backaway(obj)
end
end
elseif human then
if human.Health > 0 then
for i,x in ipairs(allies) do
if obj.Parent.Name == x then
if checkForObstacles("front") then
backaway(obj)
end
break
elseif i == #allies then
if v.Velocity.Magnitude > 15 then
human:ChangeState(Enum.HumanoidStateType.Ragdoll)
human:TakeDamage(math.random(20,30))
end
end
end
end
end
wait(0.2)
db = true
end
end
end)
table.insert(touchedEvents,touch)
end
end
function backaway(target)
pathBlocked = true
for i = 1, 8 do
wait(0.1)
myHuman:Move(-torso.CFrame.LookVector)
end
myHuman:Move(Vector3.new(0,0,0))
pathBlocked = false
end
function getUnstuck()
if not checkForObstacles("front") then
myHuman:Move(torso.CFrame.LookVector)
elseif not checkForObstacles("back") then
myHuman:Move(-torso.CFrame.LookVector)
elseif not checkForObstacles("right") then
game:GetService("TweenService"):Create(bodyBG,TweenInfo.new(0.5),{CFrame = torso.CFrame * CFrame.Angles(0,math.rad(-90),0)}):Play()
wait(0.5)
myHuman:Move(torso.CFrame.LookVector)
elseif not checkForObstacles("left") then
game:GetService("TweenService"):Create(bodyBG,TweenInfo.new(0.5),{CFrame = torso.CFrame * CFrame.Angles(0,math.rad(90),0)}):Play()
wait(0.5)
myHuman:Move(torso.CFrame.LookVector)
end
wait(0.5)
myHuman:Move(Vector3.new(0,0,0))
end
function checkDist(target)
return (myRoot.Position - target.Position).Magnitude
end
function checkSight(target)
for _, eye in pairs( {Leye, Reye} ) do
local results = workspace:Raycast( eye.Position, (target.Position - eye.Position).Unit * 120 )
if ( results and results.Instance:IsDescendantOf( target.Parent ) ) then
return true
end
end
return false
end
function findTarget()
local dist = 100
local target = nil
local potentialTargets = {}
local seeTargets = {}
for i,v in ipairs(workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
local team = v:FindFirstChild("Team")
if human and torso then
if checkDist(torso) < dist and human.Health > 0 then
if team then
if team.Value ~= myTeam.Value then
dist = checkDist(torso)
target = torso
end
else
dist = checkDist(torso)
target = torso
end
end
end
end
--Check potential targets for targets within sight
if #potentialTargets > 0 then
for i,v in ipairs(potentialTargets) do
if checkSight(v) then
table.insert(seeTargets,v)
end
end
end
--If we cant see any targets find a target that we can move to
if #potentialTargets > 0 and #seeTargets == 0 then
for i,v in ipairs(potentialTargets) do
if (torso.Position - v.Position).magnitude < dist and math.abs(torso.Position.Y - v.Position.Y) < 2 then
local path = game:GetService("PathfindingService"):CreatePath(pathArgs)
target = v
dist = (torso.Position - v.Position).magnitude
end
end
end
--If we have targets within sight target the cloest one
if #seeTargets > 0 then
dist = sight
for i,v in ipairs(seeTargets) do
if (torso.Position - v.Position).magnitude < dist then
target = v
dist = (torso.Position - v.Position).magnitude
end
end
end
return target
end
function rotate(target)
if checkDist(target,torso) > 1.5 then
local look = Vector3.new(target.Position.X,myTorso.Position.Y,target.Position.Z)
local lookDiff = (torso.CFrame.lookVector - CFrame.new(torso.Position,look).lookVector).Magnitude
game:GetService("TweenService"):Create(neck,TweenInfo.new(lookDiff),{CFrame = CFrame.new(myTorso.Position,look)}):Play()
wait(lookDiff)
end
end
function pathToTarget(target)
pathBlocked = false
local path = game:GetService("PathfindingService"):CreatePath(pathArgs)
path:ComputeAsync(torso.Position, target.Position)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for i,v in ipairs(waypoints) do
spawn(function() rotate(v) end)
local timer = 1
local success = true
repeat
wait()
myHuman:Move(torso.CFrame.LookVector)
timer = timer + 1
if timer > 40 then
success = false
break
end
until checkDist(torso,v) < 4
if success == false or pathBlocked == true then
if pathBlocked == true then
myHuman:Move(Vector3.new(0,0,0))
wait(2)
end
break
elseif (waypoints[#waypoints].Position - target.Position).magnitude > 20 then
break
elseif not target.Parent or target.Parent.Humanoid.Health <= 0 then
break
end
if i % 5 == 0 then
if checkSight(target) and checkDist(torso,target) < 50 then
break
end
end
end
myHuman:Move(Vector3.new(0,0,0))
else
failedPaths = failedPaths + 1
if failedPaths > 10 then
getUnstuck()
end
end
end
function backAway(target)
humanoid:Move(-(target.Position - head.Position).Unit)
end
function patrol()
local waypoints = workspace.waypoints:GetChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end
while wait(0.25) do
patrol()
end