Script not running in Studio but working in game?

I don’t know why but I’ve made myself a custom A* pathfinding script. But half the times i run it in studio it doesn’t work no matter how complicated the path. but from what i know its never broken in the published game.

Can someone explain why this happens?

Recently I had another studio problem where one of my scripts couldn’t generate multiple random numbers unless I added a wait(1) function. Is this related to the problem?

Other Thread: Random Number function Not working

I really dont think that that is related to the problem.

If you had a ‘while true do’ loop without a break statement or a wait() function then your studio can’t handle it.

You could try putting a wait() function into the script, i’m not sure if that will work but its worth the shot.

And can you show me the script cause that will be very usefull.

1 Like
 local function ManHattan(RoomOne, RoomTwo)
				--first
				local function Inbetween(Val, ValTwo)
					
					return (Val + (math.abs(Val-ValTwo)/2))
					
				end
				
				return math.sqrt(math.abs( Inbetween(RoomOne["Letters"][1],RoomOne["Letters"][#RoomOne["Letters"]]) - Inbetween(RoomTwo["Letters"][1],RoomTwo["Letters"][#RoomTwo["Letters"]])^2 + math.abs(RoomOne["Number"] - RoomTwo["Number"])^2))
				
			end

			local function PathFind(StartingRoom, Destination)
				
				--Initialize both open and closed list
				local OpenList={}
				local ClosedList={}
				
				--Add the start Room
				local CurrentRoom = StartingRoom
				local LevelsOfContrivance = 0
				table.insert(OpenList, StartingRoom)
				
				--Loop until end is found
				while #OpenList > 0 and LevelsOfContrivance < 100 do
					
					wait()
					LevelsOfContrivance+=1
					
					--Get the current node
					CurrentRoom = OpenList[1]

					for i = 1,#OpenList do
						
						if OpenList[i].f < CurrentRoom.f then
							
							CurrentRoom = OpenList[i]
							
						end
						
					end
					
					table.remove(OpenList, table.find(OpenList,CurrentRoom))
					table.insert(ClosedList, CurrentRoom)

					--Found the goal
					--print("CurrentRoom : ".. CurrentRoom["Name"])
					if CurrentRoom == Destination then
						
						print("Done")
						local path = {}
						local current = CurrentRoom
						local MoreContrivances = 0
						
						while current.Parent and LevelsOfContrivance < 100 do
							
							MoreContrivances+=1
							table.insert(path, current)
							current = current.Parent

						end
						
						if LevelsOfContrivance >= 100 then
							
						else
							
							return path
							
						end
						
					end
					--Generate children
					local Children = {}

					for j = 1,#OrganisedRooms do
						if CurrentRoom["Letters"][#CurrentRoom["Letters"]] + 1 == OrganisedRooms[j]["Letters"][1]  and OrganisedRooms[j]["Number"] == CurrentRoom["Number"] then --CheckAdjacentRight
							local AdjacentRight = OrganisedRooms[j]
							if AdjacentRight then
								table.insert(Children,AdjacentRight)

							end
						end
					end	
					for j = 1,#OrganisedRooms do
						if CurrentRoom["Letters"][1] - 1 == OrganisedRooms[j]["Letters"][#OrganisedRooms[j]["Letters"]] and OrganisedRooms[j]["Number"] == CurrentRoom["Number"] then --CheckAdjacentLeft
							local AdjacentLeft = OrganisedRooms[j]
							if AdjacentLeft then
								table.insert(Children,AdjacentLeft)
							end
						end
					end	

					if CurrentRoom["Name"] == "Elevator" then
						for j = 1,#OrganisedRooms do
							if (CurrentRoom["Number"] - 1 ==  OrganisedRooms[j]["Number"]) and OrganisedRooms[j]["Name"] == "Elevator" and OrganisedRooms[j]["Letters"][1] == CurrentRoom["Letters"][1] then
								table.insert(Children, OrganisedRooms[j])
							end
							if (CurrentRoom["Number"] + 1 ==  OrganisedRooms[j]["Number"]) and OrganisedRooms[j]["Name"] == "Elevator" and OrganisedRooms[j]["Letters"][1] == CurrentRoom["Letters"][1] then
								table.insert(Children, OrganisedRooms[j])

							end
						end
					end
					
					for i = 1, #Children do
						if table.find(ClosedList,Children[i]) then

						else
							Children[i].g = CurrentRoom.g +  ManHattan(Children[i],CurrentRoom)
							Children[i].h = ManHattan(CurrentRoom,Destination)
							Children[i].f = Children[i].g + Children[i].h
							
							local BreakWholeLoop = false
							
							for j = 1,#OpenList do
								
								if  Children[i] == OpenList[j] and Children[i].g >= OpenList[j].g then

									BreakWholeLoop = true
									break
									
								elseif Children[i] == OpenList[j] and Children[i].g < OpenList[j].g then

									OpenList[j].g = Children[i].g
									BreakWholeLoop = true
									
									break
									
								else

								end
							end
							
							if BreakWholeLoop == true then
								
								break
								
							end
							
							table.insert(OpenList, Children[i])	
							Children[i]["Parent"] = CurrentRoom
							
						end
						
					end		
					
				end
				
				warn("Problem with finding path: Loop at risk of running forever")
				
			end

Its a 2D version of the A* Pathfinding Algorithmn with the fact you can only move up/down if in elevator and elevator is above/below it.

Code works perfectly but just sometimes doesnt work because the “OpenList” becomes empty even when there is a path. but this only happens in studio

(Sorry ill try and explain the code cause it wasnt ment to be shown so ive just used varaible names that i understand.

[Letters"] which is still a number value but in the Grid(A-P) its the number equivilent. so if the room size is 2 and at C,D then Letters = 3,4

[“Numbers”] is the Y axis equivalent but rooms are never longer than 1 long

[“Name”] Obvious

[“Object”] the Model in the workspace of the room

[“F”],[“G”],[“H”] = Distance current room from adjacent room, Distance from adjacent room and Destination, F = G+H

[“Parent”] = The Room next to it that the path came from so it can be traced back to return the path

** )**

1 Like

Also I did try putting a wait(1) in the function but didn’t change anything

I don’t think that there are any errors why it can’t run in studio so it probably is just a bug.

Is there anyway I can fix this? Because it’s quite an important code for testing and I don’t wanna have to keep publishing and playing it each time I need to test a code

I can understand that. Honestly I don’t know how you can report bugs but here is a link for you too see how you can report bugs. And read it very carefully cause there are some rules what in your post needs to be.

1 Like

Message @Bug-Support for more support.

1 Like

Okay thanks. Annoying it’s not something I’ve done wrong lol

1 Like