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