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

How to restart a script that is running?

I'm trying to make a resource gathering AI model that collects resources from bricks. I need help with making the script restart after it has been executed once. The model moves to the resource block, shoots a beam at it and then when its full it moves to 3 checkpoints (Part 1 is the start, Part 2 is the middle/ resource dropoff, and Part 3 is the end). After it gets to part 3 it no longer works and is still looping through the second while true do loop. How could I restart the script or set up the script so it could work multiple times nomatter what. Thanks for helping!!! I don't know If I could set up some kind of coroutine to do this but if that's what you think I should do then let me know (I would probably need help with that)..Thanks

while true do
	
	wait(1)

		workspace.DeadUnits:WaitForChild("PlasticBlob")

		local Mag = (script.Parent.Torso.Position - 				workspace.DeadUnits.PlasticBlob.Blob.Position).magnitude

		print (Mag)
	

			if Mag > 15 then
			
				for i,v in pairs (workspace.DumpTruck.DumpTruck:GetChildren()) do
		
				if v:IsA "Part" then
			
				v.Anchored = false
			
				script.Parent.Humanoid:MoveTo(workspace.DeadUnits.PlasticBlob.Blob.Position)

			print "Far away"
		
		end
	end
end
			
	
		
		
if Mag < 15 then do
	
	
	for i,v in pairs (workspace.DumpTruck.DumpTruck:GetChildren()) do
		
		if v:IsA "Part" then
			
		v.Anchored = true
		
		local tool = workspace.DumpTruck.DumpTruck.Gun

			while true do
		
			wait (1)	
	
			print "While true do #2 is running"
	
			workspace.DeadUnits:WaitForChild("PlasticBlob")
	
			local Mag = (script.Parent.Torso.Position - 			workspace.DeadUnits.PlasticBlob.Blob.Position).magnitude

		
		local tool = workspace.DumpTruck.DumpTruck.Gun
		
		local Target = workspace.DeadUnits.PlasticBlob.Blob
	
	
		
		
		if tool.Parent.ResourceCarry.Value >= 0 and tool.Parent.ResourceCarry.Value < 50 and Mag < 15 then
		
		local ignore = game.Players.LocalPlayer.Character
		
		local ray = Ray.new(tool.CFrame.p, (Target.Position - tool.CFrame.p).unit * 300)
		local part, position = workspace:FindPartOnRay(ray, ignore)
 
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new("Bright red")
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.25
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
 
		local distance = (tool.CFrame.p - position).magnitude
		beam.Size = Vector3.new(0.3, 0.3, distance)
		beam.CFrame = CFrame.new(tool.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
		
		game:GetService("Debris"):AddItem(beam, 1)
		
		wait (1)
		
		--Fix lazer beam staying there after its full, make resource brick dissipear
		Target.ResourceOnDeath.Value = Target.ResourceOnDeath.Value - 1
		
		
		tool.Parent.ResourceCarry.Value = tool.Parent.ResourceCarry.Value + 1
		
		if 	tool.Parent.ResourceCarry.Value == 50 then
		
			
			v.Anchored = false
			
			workspace.DumpTruck.DumpTruck.Humanoid:MoveTo(workspace.Part1.Position) 
			
			print "Full and moving to Part 1"
			
			workspace.Part1.Touched:connect(function()
			
			workspace.DumpTruck.DumpTruck.Humanoid:MoveTo(workspace.Part2.Position) 
			
			workspace.Part2.Touched:connect(function()
			
			print "Resources Gathered!!"
	
			workspace.DumpTruck.DumpTruck.ResourceCarry.Value = 0
	
			workspace.DumpTruck.DumpTruck.Humanoid:MoveTo(workspace.Part3.Position)
			
			workspace.Part3.Touched:connect(function()
			
			--RESTART SCRIPT 
				
			end)
		end)
	end
end
end
end
end
end
end
end

2 answers

The easiest way would probably be to use a function and not have the whole script in a while loop, that way you can just call upon the start of that function whenever you need it.

Restarting a script is a sign of a larger underlying problem. Here it appears to be nested infinite loops. You can break loops with break. However, rather than use nested loops, you can try implementing a state behavior.

An example:

local searchState = 0
local gatherState = 1
local dumpState = 2

local state = searchState

while wait(1) do
	if state == searchState then
		--Search for resources
		if resourceFound then
			state = gatherState
		end
	elseif state == gatherState then	
		--Gather resources
		if inventoryFull then
			state = dumpState
		elseif resourceDepleted then
			state = searchState
		end
	elseif state == dumpState then
		--Return and dump resources
		if hasDumped then
			state = searchState
		end
	else
		warn("Invalid State!")
		state = searchState
	end
end

3 comments

0

Ok Im trying to use the script above to run my script now and Ive got it working pretty well just the last part isnt working just like above, can I message you on roblox about it?

0

Sure

0

When I tell it to go into searchstate after it has dumped it doesnt restart the script by going to the top again..why wont it?

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