RoadGenerator - A real-world road generator!

05:45:51.219 - Workspace.RoadGenerator:72: attempt to perform arithmetic (mul) on table and number

05:45:51.220 - Stack Begin

05:45:51.220 - Script ‘Workspace.RoadGenerator’, Line 72 - function genRoad

05:45:51.220 - Script ‘Workspace.RoadGenerator’, Line 135 - function generateRoads

05:45:51.221 - Script ‘rg.generateRoads(false, true, .5, 10000)’, Line 1

05:45:51.221 - Stack End

Does it work when you just run .generateRoads()?

1 Like

No, it doesnt unfortunately

sucks because it looks so good and would be so useful

Are you sure you pasted the RoadData in correctly?

This is a little confusing since it worked for others.

For me as well, I could give up on this. Generate is really good, there is another way like to make roads realistic, highways, transportations tunnels, etc?

Whoops… Is this normal? image
(edit: and this?) image

1 Like

Have you made sure you pasted the data into the RoadData multi line string correctly?

local module = {}

module.Data = [[

{

“type”: “FeatureCollection”,

“name”: “roads”,

“crs”: { “type”: “name”, “properties”: { “name”: “urn:ogc:def:crs:OGC:1.3:CRS84” } },

“features”: [
– blah blah blah
]

}

]]

return module

If that’s exactly what you’re code is, then that may be the issue. There’s no data in that.

ps, to make it easier for people to read your code on the forums make sure you format it like so:

image

Ah, sorry. And where I put “blah blah blah”, that’s all my data - it’s just pretty spammy.

And you’re 100% sure you pasted the data correctly? (and didn’t change any code in the main module?)

I haven’t seen this error before.

I pasted it all correctly. If it matters, I used Atom instead of Notepad++

Im getting length error.

image

3 Likes

Does this work with bridges and overpasses?

2 Likes

fixed the module; theres a limit of 617 roads, more than this and roblox breaks

--[[
	RoadGenerator
	Open-sourced script created by MrSprinkleToes
	Updated by Tob_Coline
	
	This version was changed on 7/22/2024.
--]]

local module = {}

local roadData = require(script.RoadData).Data

-- everything below this comment may break if you aren't sure what you're doing, so be careful!
-- if you think you probably know what you're doing, feel free to mess around with it!

function module.generateRoads(colorRoads, drawPoints, roadWidth)
	-- make sure all vars have values
	colorRoads = colorRoads or false -- default value false
	drawPoints = drawPoints or false -- default value false
	roadWidth = roadWidth or 5 -- default value 5
	local scaleMultiplier = 10000 -- must keep value at 10000
	
	local tbl = game:GetService("HttpService"):JSONDecode(roadData)
	
	local roadColors = {}
	
	local f = false
	
	local function num(model)
		local amount = 0
		
		for _,thing in pairs(model:GetChildren()) do
			amount = amount + 1
		end
		
		return amount
	end
	
	local function genRoad(number)
		local roads = Instance.new("Model", workspace)
		
		local road = tbl["features"][number]
		local properties = road["properties"]
		local roadname = properties["name"] or properties["official_name"] or "Unknown"
		local coordinates = road["geometry"]["coordinates"]
		
		-- set road color
		if colorRoads then
			roadColors[roadname] = Color3.fromRGB(math.random(0,255),math.random(0,255),math.random(0,255))
		end
		
		-- set road name
		roads.Name = roadname or properties["alt_name"] or "Unknown"
		
		local count = 1
		
		-- create & name each point of the road in the correct order
		for _,coordinate in pairs(coordinates) do
			local x = coordinate[1]
			local z = coordinate[2]
			
			
			local p = Instance.new("Part", roads)
			p.Size = Vector3.new(.5,.5,.5)
			p.Anchored = true
			p.Position = Vector3.new(x * scaleMultiplier, 0, z * scaleMultiplier)
			p.Name = count
			p.Material = Enum.Material.Neon
			p.Color = roadColors[roadname] or Color3.fromRGB(163, 162, 165)
			
			roads.PrimaryPart = p
			
			count = count + 1
		end
		
		return roads
	end
	
	local function drawRoad(model)
		local current = 1
		
		-- for each point in the road
		for i = 1, #model:GetChildren() do
			-- if there is a point after the current point
			if model:FindFirstChild(current+1) then
				local p = Instance.new("Part", model)
				p.Anchored = true
				p.Material = Enum.Material.SmoothPlastic
				if colorRoads then
					p.Color = model:FindFirstChild(current).Color
				else
					p.Color = Color3.fromRGB(27, 42, 53)
				end
				
				-- draw a part between the current & next point in the road
				local p1 = model:FindFirstChild(current).Position
				local p2 = model:FindFirstChild(current+1).Position
				local part = p
				part.CFrame = CFrame.fromMatrix((p1+p2)/2,(p2-p1).Unit,Vector3.new(0,1,0),- Vector3.new(0,1,0):Cross((p2-p1).Unit))
				part.Size = Vector3.new((p2-p1).Magnitude,.25,.5)
				
				-- make the current & next points invisible if not drawing points
				if not drawPoints then
					model:FindFirstChild(current).Transparency = 1
					model:FindFirstChild(current+1).Transparency = 1
				end
			end
			current = current + 1
		end
		
	end
	
	local function combineandteleport()
		local roads = Instance.new("Model", workspace)
		roads.Name = "Roads"
		local f = false
		for _, object in pairs(workspace:GetChildren()) do
			if object:IsA("Model") and object ~= roads then
				object.Parent = roads
			end
		end
		
		wait(5)
		
		roads:PivotTo(CFrame.new(0,0,0))
		roads:ScaleTo(roadWidth)
		
	end
	
	local MaxRoads = #tbl["features"]
	
	if MaxRoads > 610 then
		MaxRoads = 610
	end


	-- limit of 617 roads, more than this and roblox breaks
	for i=1, MaxRoads do
		local road = genRoad(i)
		drawRoad(road)
	end
	
	combineandteleport()
	
	warn("Roads generated!")
end

return module

5 Likes

Nice, but sadly only works with the Roads file, not with the buildings or landscape file