How could I combine two lua scripts into one ? I have one script for following a player and the other one following a path by default. I am trying to combine it so that if the player is in a certain distance it follows the player instead of the path. How ?
Here are the two scripts:
Following Human Script
local larm = script.Parent:FindFirstChild("Left Arm") local rarm = script.Parent:FindFirstChild("Right Arm") function findNearestTorso(pos) local list = game.Workspace:children() local torso = nil local dist = 40 local temp = nil local human = nil local temp2 = nil for x = 1, #list do temp2 = list[x] if (temp2.className == "Model") and (temp2 ~= script.Parent) then temp = temp2:findFirstChild("Torso") human = temp2:findFirstChild("Humanoid") if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then if (temp.Position - pos).magnitude < dist then torso = temp dist = (temp.Position - pos).magnitude end end end end return torso end while true do wait(0.1) local target = findNearestTorso(script.Parent.Torso.Position) if target ~= nil then script.Parent.Enemy:MoveTo(target.Position, target) end end
Pathfinding Script
Points = 4 MovingIncrement = math.random(2, 6) --Don't change anything below this line-- LastMove = 1 while true do LastMove = 1 for Move = 1, Points do Point = "Point"..tostring(LastMove).."" WalkTo = script.Parent.Parent:findFirstChild(Point) script.Parent.Humanoid:MoveTo(WalkTo.Position) LastMove = LastMove + 1 wait(MovingIncrement) end wait() end
I fixed it in studio by combining the code using conditional statements. Here is the code:
local larm = script.Parent:FindFirstChild("Left Arm") local rarm = script.Parent:FindFirstChild("Right Arm") function findNearestTorso(pos) local list = game.Workspace:children() local torso = nil local dist = 10 local temp = nil local human = nil local temp2 = nil for x = 1, #list do temp2 = list[x] if (temp2.className == "Model") and (temp2 ~= script.Parent) then temp = temp2:findFirstChild("Torso") human = temp2:findFirstChild("Humanoid") if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then if (temp.Position - pos).magnitude > dist then Points = 4 --How many points you created MovingIncrement = math.random(2, 6) --Wait in between each move (Seconds) --Don't change anything below this line-- LastMove = 1 while true do LastMove = 1 for Move = 1, Points do Point = "Point"..tostring(LastMove).."" WalkTo = script.Parent.Parent:findFirstChild(Point) script.Parent.Humanoid:MoveTo(WalkTo.Position) LastMove = LastMove + 1 wait(MovingIncrement) end wait() end elseif (temp.Position - pos).magnitude < dist then -------------------------------------------- torso = temp dist = (temp.Position - pos).magnitude end end end end return torso end while true do wait(0.1) local target = findNearestTorso(script.Parent.Torso.Position) if target ~= nil then script.Parent.Humanoid:MoveTo(target.Position, target) end end
Skillz :P