FNaF AI problem

I am trying to achieve a FNaF 1 like movement script, where the (current placeholder) part gets moved to different areas.

I am struggling majorly with getting the part to move to branch off to different areas, for example, this is how Bonnie moves in FNaF 1.
bonnie movement

Bonnie is able to pick two paths from the current location he’s in, I’m struggling to make it so he can choose between two paths instead of just one, moving it self works as long as it’s in a straight path, but when it comes to letting him choose a random number to move between two areas, I’m not sure how to go about it properly.

It currently changes the possible movement areas to a number due to the random number picker, then to nil and can’t be used by the table, what I want Bonnie to do is pick a random location out of the current possibilities he has.

I’ve attempted several methods and none have seemed to work. Such as using parts for the locations but the one I currently have is the closest I can get, though I’m still not sure how it’s supposed to work.

If I need to clarify anything further please let me know, any help would be much appreciated. I apologize for the confusing code.

	Bonnie = function()
		local movementPattern = {
			["ShowStage"] = {-28.09, 5.094, -126.425}, 
			["DiningHall"] = {-28.09, 1.422, -65.119}, 
			["BackStage"] = {-78.163, 1.422, -94.832}, 
			["LeftHall"] = {-40.066, 1.422, -27.349}, 
			["LeftCorner"] = {-40.066, 1.422, 35.079},
			["SupplyCloset"] = {-64.19, 1.422, -21.969},
			["Door"] = {-34.309, 1.422, 3.5}
		}

		local currentArea = ""
		local possibleAreas = {
			[1] = {""},
			[2] = {""}
		}
		local bonnie = workspace.Bonnie
		local bonnieTime = 4.79
		local ai = game.ReplicatedStorage.Values.AnimatronicAI.BonnieAI
		
		local function roomCheck(area)
			currentArea = area
			if currentArea == "ShowStage" then
				possibleAreas = {"DiningHall", "BackStage"}
			end
			if currentArea == "DiningHall" then
				possibleAreas = {"BackStage", "LeftHall"}
			end
			if currentArea == "BackStage" then
				possibleAreas = {"DiningHall", "LeftHall"}
			end
			if currentArea == "LeftHall" then
				possibleAreas = {"SupplyCloset", "LeftCorner"}
			end
			if currentArea == "LeftCorner" then
				possibleAreas = {"SupplyCloset", "Door"}
			end
			if currentArea == "SupplyCloset" then
				possibleAreas = {"LeftHall", "LeftCorner"}
			end

			print(possibleAreas)
			return currentArea
		end
		
		local function move(area)
			if string.match(area, roomCheck(area)) then
				local randomRoom = math.random(#possibleAreas)
				print("Hello ".. randomRoom)
				bonnie.Position = Vector3.new(unpack(movementPattern[randomRoom]))
			end
			print(currentArea)
		end
		
		move("ShowStage")
end
1 Like

Instead of having different table for the positions and possible ares, you can do this

local movementPattern = {
	Area1 = {
		Position = Vector3.new(32,0,21),
		PossibleAreas = {"Area2","Area4"}
	},
	Area2 = {
		Position = Vector3.new(32,0,21),
		PossibleAreas = {"Area1","Area3"}
	},
	Area3 = {
		Position = Vector3.new(32,0,21),
		PossibleAreas = {"Area2","Area4"}
	},
	Area4 = {
		Position = Vector3.new(32,0,21),
		PossibleAreas = {"Area1","Area4"}
	},
}

and for the movement

local function Move()
	local Rand = CurrentArea.PossibleAreas[math.random(1,#CurrentArea.PossibleAreas)]
	CurrentArea = movementPattern[Rand]
	bonnie.Position = CurrentArea.Position
end
3 Likes

Thank you so much for the help, ran into a problem though.
ServerScriptService.GameLogic.AnimatronicData:108: attempt to get length of a nil value - Server - AnimatronicData:108
Anything I should change to fix this?

Can you send me the movementpatern table?

1 Like
	local movementPattern = {
		["ShowStage"] = {
			Position = Vector3.new(-28.09, 5.094, -126.425),
			PossibleAreas = {"DiningHall","BackStage"}
		},
		["DiningHall"] = {
			Position = Vector3.new(-28.09, 1.422, -65.119),
			PossibleAreas = {"BackStage","LeftHall"}
		},
		["BackStage"] = {
			Position = Vector3.new(-78.163, 1.422, -94.832),
			PossibleAreas = {"DiningHall","LeftHall"}
		},
		["LeftHall"] = {
			Position = Vector3.new(-40.066, 1.422, -27.349),
			PossibleAreas = {"LeftCorner","SupplyCloset"}
		},
		["LeftCorner"] = {
			Position = Vector3.new(-40.066, 1.422, 35.079),
			PossibleAreas = {"SupplyCloset","Door"}
		},
		["SupplyCloset"] = {
			Position = Vector3.new(-64.19, 1.422, -21.969),
			PossibleAreas = {"LeftHall","Door"}
		},
		["Door"] = {
			Position = Vector3.new(-34.309, 1.422, 3.5),
		},
	}
1 Like

Door doesnt have any possible areas i guess you need to to some check like

local function Move()
	if not CurrentArea.PossibleAreas then return end
	local Rand = CurrentArea.PossibleAreas[math.random(1,#CurrentArea.PossibleAreas)]
	CurrentArea = movementPattern[Rand]
	bonnie.Position = CurrentArea.Position
end
2 Likes

it might be something to do with currentArea…? the print believes currentArea has nothing within it and Bonnie doesn’t actually move whatsoever

yea thats the issue, because “Door” doesnt have and Possible ares and it just errors

1 Like

do i give door any possible areas? i mean, since Bonnie is supposed to get sent back if the door is shut, do I just give it a possibility of a single room so he can teleport back if the door was closed?

yea i guess you can do that (29 chorecters)

1 Like

tried doing that, i think currentRoom isn’t tied to Bonnie which i think is causing the problems

Bonnie’s current room is: and his current position is: -87.27999877929688, 0.5000039935112, 229.6699981689453 - Server - AnimatronicData:116

the position is in the same as where the original part is placed which is outside the map, the current room part is blank because it’s " ", there’s nothing in it because nothing inserts anything into it, do you think there’s anything i could try else?

wait, was i supposed to remove the function to do where he checks where he is?

edit, added it back and removed the check so the function could at least run, i think currentRoom is being swapped to a nil value

is there anything related to currentRoom im supposed to add outside of the move function? it doesn’t work due to currentRoom not working

update, i’ve solved it with some help now, closing this post