I'm trying to make an procedurally generated runner but I've run into a few problems. These path models aren't generating forward. The only thing working is deleting the previous model. I've used :MoveTo() to move the model and I used the road's position so I can add it by the 'path length'. I have a module and server script, the server script is fired when the player touches the road.
The Module Script:
local generateModule = {}
local replicatedStorage = game:GetService('ReplicatedStorage')
local serverStorage = game:GetService('ServerStorage')
local pathLength = 80
local plainPaths = serverStorage:WaitForChild('PlainPaths'):GetChildren()
local obstructionPaths = serverStorage:WaitForChild('ObstructionPaths'):GetChildren()
generateModule.GenerateNewPath = function (currentPathNumber, currentPath)
local decidedPathType = math.random(1, 2)
if decidedPathType == 1 then
decidedPath = plainPaths[math.random(1, #plainPaths)]
else
decidedPath = obstructionPaths[math.random(1, #obstructionPaths)]
end
decidedPath:Clone().Parent = workspace
decidedPath.PathNumber.Value = currentPathNumber + 1
decidedPath:MoveTo(currentPath.Road.Position + Vector3.new(0, 0, pathLength)) -- = currentPath.Road.Position + Vector3.new(0, 0, pathLength)
local decidedPathType2 = math.random(1, 2)
if decidedPathType2 == 1 then
decidedPath2 = plainPaths[math.random(1, #plainPaths)]
else
decidedPath2 = obstructionPaths[math.random(1, #obstructionPaths)]
end
decidedPath2:Clone().Parent = workspace
decidedPath2.PathNumber.Value = decidedPath.PathNumber.Value + 1
decidedPath2:MoveTo(decidedPath.Road.Position + Vector3.new(0, 0, pathLength))--= decidedPath.Road.Position + Vector3.new(0, 0, pathLength)
local lastPath = currentPathNumber - 1
for _, path in pairs (workspace:GetChildren()) do
if path.Name == 'Path1' or path.Name == 'Path2' or path.Name == 'Path3' or path.Name == 'Path4' then
if path.PathNumber.Value == lastPath then
path:Destroy()
end
end
end
end
return generateModule
The Server Script:
local serverStorage = game:GetService('ServerStorage')
local serverScriptService = game:GetService('ServerScriptService')
local replicatedStorage = game:GetService('ReplicatedStorage')
local generationModule = require(serverScriptService.GenerationSettings)
local path = script.Parent.Parent
local pathNumber = path:WaitForChild('PathNumber')
function onTouch(hit)
local humanoid = hit.Parent:FindFirstChild('Humanoid')
if pathNumber.Value > 3 and humanoid then
generationModule.GenerateNewPath(pathNumber.Value, path)
script.Disabled = true
end
end
script.Parent.Touched:connect(onTouch)
