Chunk generation script not working properly

Basically, The Chunk was supposed to be generated with an offset from the position of another chunk, and if there’s an chunk alreadly there it shouldn’t generate.

The Problem is that, after the first chunk generates, when i step on the next chunk it generates on the same spot, i did some testing and the position variable on onTouch is always 0,0,0.

I Have alreadly tried changing the values to new vector 3’s but they don’t seem to work.

External Media
--Constant Values
--local MAX_DISTANCE = 1000
local Event = workspace:WaitForChild("ChunkTouched")
local chunks = game.ReplicatedStorage:WaitForChild("Chunks")
local player = game:GetService("Players")
--Variable Values
local chunkNum = 0
local Offset = 100
--Arrays
local chunkPrefabs = chunks:GetChildren()
local gChunks = workspace.gChunks
local chunksPosition = {}

local function generateChunk(xOffset,zOffset) 
	local selectedChunk = math.random(1,#chunkPrefabs)
	local geneChunk = chunkPrefabs[selectedChunk]:Clone()

	--Chunk position
	chunkNum += 1
	geneChunk.Parent = workspace.gChunks
	geneChunk.Name = chunkNum
	geneChunk:SetPrimaryPartCFrame(CFrame.new(xOffset, 0, zOffset))

	chunksPosition[chunkNum] = geneChunk.PrimaryPart.Position
end

local function onTouch(num)
	local position = Vector3.new(chunksPosition[num])
	local wantedPositionX = position.X + Offset
	local wantedPositionZ = position.Z + Offset
	for i=1,#chunksPosition do
		if i > 1 and chunksPosition[i] == position then
			print(position)
		elseif i == #chunksPosition then
			generateChunk(wantedPositionX,0)
		end 
	end
	--generateChunk(0,position.Z + Offset)
	--generateChunk(position.X - Offset,0)
	--generateChunk(0,position.Z - Offset)
end

--Start (When the game Loads)
generateChunk(0,0)
print(chunksPosition)
--Start End

Event.Event:Connect(onTouch)

Here’s the script that’s inside the chunk’s floor.

local Event = workspace:WaitForChild("ChunkTouched")
local cooldown = 2
local hastouched = false

if not hastouched then
	script.Parent.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") ~= nil and hastouched == false then
			local name = tonumber(script.Parent.Parent.Name)

			hastouched = true
			Event:Fire(name)
		end
	end)
end

task.wait(cooldown)
hastouched = false
3 Likes
local position = Vector3.new(chunksPosition[num])

has to be changed to

local position = chunksPosition[num]

in about line 29.
Also remember to remove the for loop after doing that, so it can generate more than 2 chunks again

Hey there, I’m not home right now but I’ll share this here.

I’ll explain the system to you later if you want.

lel the script he is making is also for backrooms game

Well perhaps it can help me solve this, how did you make this work??

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