I'm making a game that uses a resource collecting model to collect resources then deposit them after its full but I'm having a problem making the script below restart. At the end when it touches part 3 it sets the state of the model to "searchState" so the script can complete again from the top but its not working. What am I doing wrong? Thanks for the help


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

local state = searchState

while wait(1) do

	print "Checking state"

	if state == searchState then
--Search for resources
		workspace.DeadUnits:WaitForChild("PlasticBlob")

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

		print (Mag)

		if Mag > 15 then

			print "Far away"

			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)

				end
			end
		end

		if Mag < 15 then do

			print "Arrived"
	
			for i,v in pairs (workspace.DumpTruck.DumpTruck:GetChildren()) do

				if v:IsA "Part" then

					v.Anchored = true

					resourceFound = true

					end
				end
			end
		end
	end

	if resourceFound == true then

		state = gatherState

		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

			print "Gathering..."

			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)

			Target.ResourceOnDeath.Value = Target.ResourceOnDeath.Value - 1

			tool.Parent.ResourceCarry.Value = tool.Parent.ResourceCarry.Value + 1

		elseif tool.Parent.ResourceCarry.Value == 50 then

			break end

			inventoryFull = true
			
			end
		end 

	if inventoryFull == true then

		state = dumpState

		print "Moving to Dump"

		for i,v in pairs (workspace.DumpTruck.DumpTruck:GetChildren()) do

			if v:IsA "Part" then

				v.Anchored = false
		
				workspace.DumpTruck.DumpTruck.Humanoid:MoveTo(workspace.Part1.Position) -----

			end
		end

		
		workspace.Part1.Touched:connect(function()

			local player = game.Players.LocalPlayer

			workspace.DumpTruck.DumpTruck.Humanoid:MoveTo(workspace.Part2.Position) 
		
		workspace.Part2.Touched:connect(function()

			print "Resources Gathered!!"

			workspace.DumpTruck.DumpTruck.ResourceCarry.Value = 0

			player.leaderstats.Plastic.Value = player.leaderstats.Plastic.Value + 150  
			
			workspace.DumpTruck.DumpTruck.Humanoid:MoveTo(workspace.Part3.Position)

		workspace.Part3.Touched:connect(function()
--]]
			print "searchState"

			state = searchState
		end)
	end)
end)
end



2 comments

1

FORMAT IT BETTER. I cannot debug what I cannot read. Even no formatting is better than bad formatting.

0

Updated, I hope this is better If not let me know..Thanks