I am trying to make a rooms-type game on roblox, and I have the module to make the monster navigate. However, if a player goes into the next room the monster will not follow him into the next room and despawn, so the players can avoid the monster by going foward rather than hiding in a locker. I know why this happens, it’s because the for loop’s maxNum value doesn’t get updated when maxNum changes. Is there anyway to fix the loop, or another way to fix my issue?
function monsterSpawning.Navigate(model, prevNum, maxNum, generatedRooms, speed)
local help = workspace.GeneratedRooms.ChildAdded:Connect(function()
maxNum += 1--my attempt at fixing it
end)
for i=prevNum, maxNum do--example: prevNum is 3 and maxNum is 5, but maxNum changes to 6 when the childadded function is called
local room = generatedRooms[i]--table of all of the rooms that are generated
local waypoints = room:FindFirstChild("Waypoints")
monsterSpawning.Move(model, room.Entrance, speed)
if waypoints then
for i=1, #waypoints:GetChildren() do--if theres waypoints in the room, follow them
monsterSpawning.Move(model, waypoints[i], speed)
end
end
monsterSpawning.Move(model, room.Exit, speed)
end
help:Disconnect()
end
I have been stumped on this for a while. It would be awesome if someone could help.
I don’t know what the variables or classes mean, but here’s my wild guess or attempt at fixing it using recursion:
function monsterSpawning.Navigate(model, prevNum, maxNum, generatedRooms, speed)
local updatedMaxNum = maxNum
local help = workspace.GeneratedRooms.ChildAdded:Connect(function()
updatedMaxNum += 1
end)
local iterate
function iterate(prevNum, maxNum)
for i=prevNum, maxNum do--example: prevNum is 3 and maxNum is 5, but maxNum changes to 6 when the childadded function is called
local room = generatedRooms[i]--table of all of the rooms that are generated
local waypoints = room:FindFirstChild("Waypoints")
monsterSpawning.Move(model, room.Entrance, speed)
if waypoints then
for i=1, #waypoints:GetChildren() do--if theres waypoints in the room, follow them
monsterSpawning.Move(model, waypoints[i], speed)
end
end
monsterSpawning.Move(model, room.Exit, speed)
if maxNum < updatedMaxNum then
iterate(maxNum, updatedMaxNum)
maxNum = updatedMaxNum
break
else
break
end
end
end
iterate(prevNum, maxNum)
help:Disconnect()
end
--I HOPE THIS CODE RUNS D:
bye, good luck with your game
edit, if this does not work try:
function iterate(prevNum, maxNum2)
for i=prevNum, maxNum2 do
I had to remove this part because once the navigate function is finished, the monster despawns. It gets finished once that loop is complete.
Another thing, this doesn’t work because the monster simply moves back to the room it started at (which is Room[prevNum].
This was also very similar to one of my attempts to fix, but behaved a little bit better
I see. Well, can’t help you from there. Unless if you want me to see the whole workflow, then I give a shot. But here’s a last-resort tip: have a separate function or script that compels the monster to move to a specific location or part. And another function or script managing the location of that part. I think this division of task will make it easier to catch bugs