My auto generated roads not working properly

i have this script and i wanna make it so that creates roads everytime the player enters or touched the detect part, it should’ve generate a straight roads
only problem is that it only generated once and never agian

theres no errors aswell so i dont know what’s the problem of it.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RoadFolder = workspace:FindFirstChild("RoadFolder")
local RoadTemplate = workspace:FindFirstChild("RoadTemplate")
local Detect = RoadTemplate:FindFirstChild("Detect")
local EndPoint = RoadTemplate:FindFirstChild("EndPoint")

local NumberValue = ReplicatedStorage:FindFirstChild("NumberValue")

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:FindFirstChildOfClass("Humanoid")

		local function CreateRoad(previousEndPoint)
			local RoadClone = RoadTemplate:Clone()
			local DetectClone = RoadClone:FindFirstChild("Detect")
			local EndPointClone = RoadClone:FindFirstChild("EndPoint")

			RoadClone:SetPrimaryPartCFrame(CFrame.new(previousEndPoint.Position))
			RoadClone.Parent = RoadFolder
			RoadClone.Name = "Road " .. NumberValue.Value

			NumberValue.Value = NumberValue.Value + 1

			if DetectClone and Humanoid then
				DetectClone.Touched:Connect(function()
					if #RoadFolder:GetChildren() > 1 then
						CreateRoad(EndPointClone)
					end
				end)
				DetectClone.TouchEnded:Connect(function()
					if #RoadFolder:GetChildren() == NumberValue.Value + 1 then
						RoadClone:Destroy()
					end
				end)
			end
		end

		if Detect and Humanoid then
			Detect.Touched:Connect(function()
				if #RoadFolder:GetChildren() == 0 then
					CreateRoad(EndPoint)
				end
			end)
		end
	end)
end)

#RoadFolder:GetChildren() > 1 then

This might be the issue?
Try using >= instead of > and see if it resolves the issue

1 Like

You are checking for the Humanoid too soon. Better check it after the player touches detect
You should also disconnect the Touch event after it activates. You don’t want players to activate it multiple times even though the road has already generated. Also add a debounce.

2 Likes