Hi, so I have a script that gets plates (they're models, not parts) from ReplicatedStorage that I like to call "chunks". I'm trying to make them not overwrite each other by checking if another chunk has the same X and Z position as the new chunk and destroying the new one if so, but they still overlap each other.
Also, one chunk in the corner (relative to chunk "0", the chunk generated from) doesn't generate.
Script:
local storage = game:GetService("ReplicatedStorage")
function newChunk(id)
local chunks = storage.ChunkTypes:GetChildren()
if id == 1 then
local newPos = Vector3.new(script.Parent.Position.X + 256,
script.Parent.Position.Y,
script.Parent.Position.Z
)
local chunkTypes = storage.ChunkTypes:GetChildren()
local newChunk = chunkTypes[math.random(1,#chunkTypes)]:Clone()
for _, each in ipairs(workspace.Chunks:GetChildren()) do
if each.Base.Position == newPos then
newChunk:Destroy()
else
newChunk.Parent = workspace.Chunks; newChunk.Name = newChunk.Name..workspace.NextChunk.Value; newChunk:MoveTo(newPos)
end
end
elseif id == 2 then
local newPos = Vector3.new(script.Parent.Position.X - 256,
script.Parent.Position.Y,
script.Parent.Position.Z
)
local chunkTypes = storage.ChunkTypes:GetChildren()
local newChunk = chunkTypes[math.random(1,#chunkTypes)]:Clone()
for _, each in ipairs(workspace.Chunks:GetChildren()) do
if each.Base.Position == newPos then
newChunk:Destroy()
else
newChunk.Parent = workspace.Chunks; newChunk.Name = newChunk.Name..workspace.NextChunk.Value; newChunk:MoveTo(newPos)
end
end
elseif id == 3 then
local newPos = Vector3.new(script.Parent.Position.X,
script.Parent.Position.Y,
script.Parent.Position.Z + 256
)
local chunkTypes = storage.ChunkTypes:GetChildren()
local newChunk = chunkTypes[math.random(1,#chunkTypes)]:Clone()
for _, each in ipairs(workspace.Chunks:GetChildren()) do
if each.Base.Position == newPos then
newChunk:Destroy()
else
newChunk.Parent = workspace.Chunks; newChunk.Name = newChunk.Name..workspace.NextChunk.Value; newChunk:MoveTo(newPos)
end
end
elseif id == 4 then
local newPos = Vector3.new(script.Parent.Position.X,
script.Parent.Position.Y,
script.Parent.Position.Z - 256
)
local chunkTypes = storage.ChunkTypes:GetChildren()
local newChunk = chunkTypes[math.random(1,#chunkTypes)]:Clone()
for _, each in ipairs(workspace.Chunks:GetChildren()) do
if each.Base.Position == newPos then
newChunk:Destroy()
else
newChunk.Parent = workspace.Chunks; newChunk.Name = newChunk.Name..workspace.NextChunk.Value; newChunk:MoveTo(newPos)
end
end
elseif id == 5 then
local newPos = Vector3.new(script.Parent.Position.X + 256,
script.Parent.Position.Y,
script.Parent.Position.Z + 256
)
elseif id == 6 then
local newPos = Vector3.new(script.Parent.Position.X - 256,
script.Parent.Position.Y,
script.Parent.Position.Z - 256
)
local chunkTypes = storage.ChunkTypes:GetChildren()
local newChunk = chunkTypes[math.random(1,#chunkTypes)]:Clone()
for _, each in ipairs(workspace.Chunks:GetChildren()) do
if each.Base.Position == newPos then
newChunk:Destroy()
else
newChunk.Parent = workspace.Chunks; newChunk.Name = newChunk.Name..workspace.NextChunk.Value; newChunk:MoveTo(newPos)
end
end
elseif id == 7 then
local newPos = Vector3.new(script.Parent.Position.X + 256,
script.Parent.Position.Y,
script.Parent.Position.Z - 256
)
local chunkTypes = storage.ChunkTypes:GetChildren()
local newChunk = chunkTypes[math.random(1,#chunkTypes)]:Clone()
for _, each in ipairs(workspace.Chunks:GetChildren()) do
if each.Base.Position == newPos then
newChunk:Destroy()
else
newChunk.Parent = workspace.Chunks; newChunk.Name = newChunk.Name..workspace.NextChunk.Value; newChunk:MoveTo(newPos)
end
end
elseif id == 8 then
local newPos = Vector3.new(script.Parent.Position.X - 256,
script.Parent.Position.Y,
script.Parent.Position.Z + 256
)
local chunkTypes = storage.ChunkTypes:GetChildren()
local newChunk = chunkTypes[math.random(1,#chunkTypes)]:Clone()
for _, each in ipairs(workspace.Chunks:GetChildren()) do
if each.Base.Position == newPos then
newChunk:Destroy()
else
newChunk.Parent = workspace.Chunks; newChunk.Name = newChunk.Name..workspace.NextChunk.Value; newChunk:MoveTo(newPos)
end
end
end
end
script.Parent.Touched:connect(function(h)
if game.Players:GetPlayerFromCharacter(h.Parent) then
for i = 1,8 do
newChunk(i)
end
workspace.NextChunk.Value = workspace.NextChunk.Value + 1
script:Destroy()
end
end)
It may seem kinda big, but if you help me, you get a free figurative, nonexistent cookie.
Help, please?

1 comment
Thank you :)