Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Combining two scripts into one ?[Answered]

Asked by 10 years ago

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
1
This is a non-trivial combination. It's made much harder when the scripts were not intended for use as APIs. BlueTaslem 18071 — 10y

1 answer

Log in to vote
0
Answered by 10 years ago

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

0
I hope this is helpful to anyone. I also forgot I had the original follow script and this script and the npc as the parent in order for it to work. TechProdigy 20 — 10y
Ad

Answer this question