as you see in the video, i wanted chunks generate perfectly without unexpected gaps/delay
(main train not moving for some players if they lag they would fall)
i dont know any better way to make there isnt gaps yet…
i tried make newChunk PivotTo prevChunk CFrame,
but somehow more chunks generated more offset whole map gets
so i hope anyone helps/give idea to tell how should i fix that
heres code:
- Server
local RS = game:GetService("ReplicatedStorage")
local SS = game:GetService("ServerStorage")
local TS = game:GetService("TweenService")
local RoundFolder = SS:WaitForChild("Rounds")
local ChunkModule = require(script.ChunkModule)
local CanGenerateChunk = true
local ChunkFolder = workspace.ChunkFolder
local ChunkNum = 0
local MaxChunk = 5
local StudsPerSec = 2000
local function RoundSetUp()
local Rounds = RoundFolder:GetChildren()
local selectedRound = Rounds[math.random(1, #Rounds)]
print(selectedRound)
local MapFolder = selectedRound.MapFolder:GetChildren()
if selectedRound then
for i = 1, #MapFolder do
print(MapFolder[i])
local Map = MapFolder[i]
local PrevChunk = Map:FindFirstChild("ChunkStart"):Clone()
PrevChunk.Parent = ChunkFolder
PrevChunk.Name = "Chunk0"
PrevChunk:PivotTo(workspace.SpawnPos.CFrame)
TS:Create(PrevChunk.PrimaryPart, TweenInfo.new(20,Enum.EasingStyle.Linear), {CFrame = PrevChunk.PrimaryPart.CFrame - PrevChunk.PrimaryPart.CFrame.LookVector*StudsPerSec}):Play()
while CanGenerateChunk do
local discardChunk = ChunkNum - MaxChunk
if discardChunk > -1 then
ChunkFolder:FindFirstChild("Chunk"..discardChunk):Destroy()
end
task.wait(2)
ChunkNum += 1
PrevChunk = ChunkModule.Generate(PrevChunk ,Map, ChunkNum, StudsPerSec, WaitTime)
end
end
end
end
task.wait(1)
RoundSetUp()
- Module
local RS = game:GetService("ReplicatedStorage")
local SS = game:GetService("ServerStorage")
local TS = game:GetService("TweenService")
local RoundFolder = SS:WaitForChild("Rounds")
local Chunk = {}
Chunk.Info = require(script.chunk_Info)
Chunk.random = Random.new()
function Chunk.selectRandom(Map)
local totalWeight = 0
for i, info in pairs(Chunk.Info) do
totalWeight += info.Weight
end
local randomWeight = Chunk.random:NextInteger(1, totalWeight)
local currentWeight = 0
local randomChunk = nil
for i, info in pairs(Chunk.Info) do
currentWeight += info.Weight
if randomWeight <= currentWeight then
randomChunk = Map[i]
print(randomChunk)
break
end
end
return randomChunk
end
function Chunk.Generate(PrevChunk:Model, Map:Folder, ChunkNum:number, StudsPerSec:number, WaitTime:number)
local randomChunk = Chunk.selectRandom(Map)
local newChunk = randomChunk:Clone()
newChunk.PrimaryPart = newChunk.Primary
newChunk.Name = "Chunk"..ChunkNum
--newChunk.Primary.Transparency = 1
if newChunk:FindFirstChild("Start") then
--newChunk.End.Transparency = 1
end
newChunk.Parent = workspace.ChunkFolder
newChunk:PivotTo(workspace.SpawnPos.CFrame)
TS:Create(newChunk.PrimaryPart, TweenInfo.new(20,Enum.EasingStyle.Linear), {CFrame = newChunk.PrimaryPart.CFrame - newChunk.PrimaryPart.CFrame.LookVector*StudsPerSec}):Play()
return newChunk
end
return Chunk