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

Why does my script get to the second if statement then stop? [Unanswered]

Asked by
Prioxis 673 Moderation Voter
8 years ago

Okay it was like 1 in the morning when I wrote this so sorry if it looks terrible but the script is a roaming script for a humanoid that tells it to move to a node on the map once the magnitude between the npc and the node are less than 3 I want it to go to the next node and then make that check until it gets within 3 studs of that node...

--roaming script by darrion22
local human = script.Parent.Humanoid
local torso = script.Parent.Torso
local running = false
local Print = print
local Node1 = game.Workspace.Node1
local Node2 = game.Workspace.Node2
local Node3 = game.Workspace.Node3
local Node4 = game.Workspace.Node4
local Node5 = game.Workspace.Node5
    -- Don't code while tired kids
    local mag = (torso.Position - Node1.Position).magnitude
    local mag2 = (torso.Position - Node2.Position).magnitude
    local mag3 = (torso.Position - Node3.Position).magnitude 
    local mag4 = (torso.Position - Node4.Position).magnitude    
    local mag5 = (torso.Position - Node5.Position).magnitude

local moved1 = false
local moved2 = false
local moved3 = false-- don't recall why I put this in
local moved4 = false
local moved5 = false    -- or this one

    while wait(1) do 
    if running == true then
        print("Script is running")
        else
        running = true
        human:MoveTo(Node1.Position)  
        Print('moving to Node1')
    if mag <= 3 then
        moved1 = true
        human:MoveTo(Node2.Position)
        Print('moving to Node2')
    if mag2 <= 3 then
        human:MoveTo(Node3.Position)
        Print('moving to Node3')
    if mag3 <= 3 then
        human:MoveTo(Node4.Position)
        Print('moving to Node4')
    if mag4 <= 3 then
        human:MoveTo(Node5.Position)
        Print('moving to Node5')
    if mag5 <= 3 then

                        end
                    end
                end
            end
        end
    end 
end

-- why can't I have coffee

1 answer

Log in to vote
-1
Answered by 8 years ago

This indeed is very ugly, so I'd rather begin by simplifying it. From looking at your current code, it doesn't work because you check distance between nodes only at beginning of your code, so when your character starts moving, all the distances will be outdated.

Also, what is going on with your wonky spacing/tabbing?

local nodes = { game.Workspace.Node1, game.Workspace.Node2, game.Workspace.Node3, game.Workspace.Node4, game.Workspace.Node5 }

--roaming script by darrion22
local human = script.Parent.Humanoid
local torso = script.Parent.Torso
local running = false
local Print = print
local targetNode = 1

while wait(1) do 
    if running then
        print("Script is running")
        if (nodes[targetNode].Position - torso.Position).magnitude < 3 then
            -- This line might seem like witchcraft, but it increases targetNode by one, but if the number becomes bigger than number of nodes, it starts counting from 1 again.
            targetNode = targetNode%#nodes+1 
            running = false
        end
    else
        running = true
        human:MoveTo( nodes[targetNode].Position )
            Print( 'moving to '..nodes[targetNode].Name )
    end 
end

-- why can't I have coffee

0
Okay I thought so sorry for it being ugly I was tired af and didn't realize what I was doing Prioxis 673 — 8y
0
he still only gets to the first node then stops Prioxis 673 — 8y
Ad

Answer this question