Errors on generating rooms!

I’ve been trying to script a room generating system from a YouTube video, but even though I copied the code, I don’ get the same results as him. What’s the problem?

The code is found at time, 20:30

YouTube Video: https://www.youtube.com/watch?v=QLoF9Er8Oo8&list=PLtMUa6NlF10e1UqysaWEVAUDTFHdBBMtH&index=2

My code in “Room” module script:

local room = {}
room.info = require(script.RoomInfo)
room.lastTurnDirection = nil
room.random = Random.new()

function room.GetRandom(prevRoom)
	local totalWeight = 0
	for i, info in pairs(room.info) do
		totalWeight += info.Weight
	end
	
	local randomWeight = room.random:NextNumber(0,totalWeight)
	local currentWeight = 0
	local randomRoom = nil
	for i, info in pairs(room.info) do
		currentWeight += info.Weight
		if randomRoom <= currentWeight then
			randomRoom = workspace.Rooms[i]
			break
		end
	end
	
	--Room must be different from previous room
	--of orner, next turn must be different
	--if previous room is stairs, then no stairs on the next
	
	local direction = room.info[randomRoom.Name]["Direction"]
	local hasStairs = room.info[randomRoom.Name]["Stairs"]
	local prevHadStairs = room.info[prevRoom.Name]["Stairs"]
	
	if (prevRoom.Name == randomRoom.Name) 
		or (direction and direction == room.lastTurnDirection )
		or (hasStairs and prevHadStairs )
	then
		return room.GetRandom(prevRoom)
	else
		if direction then
			room.lastTurnDirection = direction
		end
		
		return randomRoom
	end
	
	
end

function room.Generate(prevRoom)
	local randomRoom = room.GetRandom(prevRoom)
	local newRoom = randomRoom:Clone()
	
	newRoom.PrimaryPart = newRoom.Entrance
	newRoom:PivotTo(prevRoom.Exit.CFrame)
	newRoom.Entrance.Transparency = 1
	newRoom.Exit.Transparency = 1
	
	newRoom.Parent = workspace.GeneratedRooms
	
	return newRoom
end

return room

Explorer layout:

What line does your error occur?

It just says that it cannot calculate math on “Random room”

Can you take a screenshot of the error?

I can in an hour from now. Its a fairly simple error.

Here is the error
image

I wrote the code wrong:

New Module Script

local room = {}
room.info = require(script.RoomInfo)
room.lastTurnDirection = nil
room.random = Random.new()

function room.GetRandom(prevRoom)
	local totalWeight = 0
	for i, info in pairs(room.info) do
		totalWeight += info.Weight
	end
	
	local randomWeight = room.random:NextInteger(1,totalWeight)
	local currentWeight = 0
	local randomRoom = nil
	for i, info in pairs(room.info) do
		currentWeight += info.Weight
		if randomWeight <= currentWeight then
			randomRoom = workspace.Rooms[i]
			break
		end
	end
	
	--Room must be different from previous room
	--if right turn, next turn must be different
	--if previous room is stairs, then no stairs on the next
	
	local direction = room.info[randomRoom.Name]["Direction"]
	local hasStairs = room.info[randomRoom.Name]["Stairs"]
	local prevHadStairs = room.info[prevRoom.Name]["Stairs"]
	
	if (prevRoom.Name == randomRoom.Name) 
		or (direction and direction == room.lastTurnDirection )
		or (hasStairs and prevHadStairs )
	then
		return room.GetRandom(prevRoom)
	else
		if direction then
			room.lastTurnDirection = direction
		end
		
		return randomRoom
	end
	
	
end

function room.Generate(prevRoom)
	local randomRoom = room.GetRandom(prevRoom)
	local newRoom = randomRoom:Clone()
	
	newRoom.PrimaryPart = newRoom.Entrance
	newRoom:PivotTo(prevRoom.Exit.CFrame)
	newRoom.Entrance.Transparency = 1
	newRoom.Exit.Transparency = 1
	
	newRoom.Parent = workspace.GeneratedRooms
	
	return newRoom
end

return room

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.