Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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

2 comments

0

Okay I thought so sorry for it being ugly I was tired af and didn't realize what I was doing

0

he still only gets to the first node then stops

This discussion is preserved as it was. New answers, comments, and votes are closed.