Hello! I’m making this thread to know how I can improve my “AI” System. What it does is basically follow the player. My code:
wait(5)
--\\VARIABLES://--
local Character = script.Parent
local Humanoid = Character.Humanoid
local RegionPart = Character.RegionPart
local InitialPoint = Character.Torso.Position
local MoveToStart
local ChasingCharacterName = ""
local WalkAnimation
--\\END OF VARIABLES.//--
Humanoid:SetStateEnabled(Enum.HumanoidStateType.Ragdoll,false)
Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing,false)
Humanoid:SetStateEnabled(Enum.HumanoidStateType.RunningNoPhysics,false)
Humanoid:SetStateEnabled(Enum.HumanoidStateType.Seated,false)
--\\THREAD SECTION://--
local DetectThread = coroutine.wrap(function()
while wait(0.1) do
local Region = Region3.new(RegionPart.Position - (RegionPart.Size/2), RegionPart.Position + (RegionPart.Size/2))
local Parts = workspace:FindPartsInRegion3WithIgnoreList(Region, Character:GetChildren())
for i,v in pairs(Parts) do
if v.Parent:FindFirstChild("Humanoid") and v.Parent.Parent ~= workspace["Passive NPCS"] then
if ChasingCharacterName ~= "" then
if ChasingCharacterName == v.Parent.Name then
ChasingCharacterName = v.Parent.Name
Humanoid:MoveTo(v.Parent.Torso.Position)
print("Found a character. Character name: "..v.Parent.Name)
MoveToStart()
end
end
end
end
end
end)
DetectThread()
function WaitThreadStopMoving()
local CWaitThread = coroutine.wrap(function()
wait(5)
if ChasingCharacterName == "" then
MoveToStart()
Humanoid:MoveTo(InitialPoint)
print("Moving to Initial Point...")
end
end)
CWaitThread()
end
--\\END OF THREAD SECTION.//--
--\\ANIMATION AND EVENTS SECTION://--
local AnimationInstanceWalk = Instance.new("Animation")
AnimationInstanceWalk.AnimationId = "rbxassetid://180426354"
WalkAnimation = Humanoid:LoadAnimation(AnimationInstanceWalk)
MoveToStart = function()
if WalkAnimation.IsPlaying == false then
WalkAnimation:Play()
end
end
Humanoid.MoveToFinished:Connect(function()
ChasingCharacterName = ""
WaitThreadStopMoving()
WalkAnimation:Stop()
end)
--\\END OF ANIMATION AND EVENTS SECTION.//--
Any help is appreciated!