So i just made a chunk-loading system. When it load/unload chunks the code is very repetitive. Is there any way to make this shorter with a for loop or something?
Thanks
local RepStore = game:GetService("ReplicatedStorage")
local children = workspace:WaitForChild("Chunks"):GetChildren()
local player = game.Players.LocalPlayer
for i = 1, #children do
children[i].Name = math.floor(children[i].Position.x/100 + .5) .. ", " .. math.floor(children[i].Position.z/100 + .5)
children[i].Parent = RepStore.Chunks
end
wait(1)
while wait(1) do
local chords = player.Character.HumanoidRootPart.Position
local x = math.floor(chords.x/100 + 0.5)
local y = math.floor(chords.y/100 + 0.5)
print(x, y)
if RepStore.Chunks:FindFirstChild(x .. ", " .. y) then
RepStore.Chunks:FindFirstChild(x .. ", " .. y).Parent = workspace.Chunks
end
if RepStore.Chunks:FindFirstChild(x+1 .. ", " .. y) then
RepStore.Chunks:FindFirstChild(x+1 .. ", " .. y).Parent = workspace.Chunks
end
if RepStore.Chunks:FindFirstChild(x .. ", " .. y+1) then
RepStore.Chunks:FindFirstChild(x .. ", " .. y+1).Parent = workspace.Chunks
end
if RepStore.Chunks:FindFirstChild(x+1 .. ", " .. y+1) then
RepStore.Chunks:FindFirstChild(x+1 .. ", " .. y+1).Parent = workspace.Chunks
end
if RepStore.Chunks:FindFirstChild(x-1 .. ", " .. y) then
RepStore.Chunks:FindFirstChild(x-1 .. ", " .. y).Parent = workspace.Chunks
end
if RepStore.Chunks:FindFirstChild(x .. ", " .. y-1) then
RepStore.Chunks:FindFirstChild(x .. ", " .. y-1).Parent = workspace.Chunks
end
if RepStore.Chunks:FindFirstChild(x-1 .. ", " .. y-1) then
RepStore.Chunks:FindFirstChild(x-1 .. ", " .. y-1).Parent = workspace.Chunks
end
if RepStore.Chunks:FindFirstChild(x-1 .. ", " .. y+1) then
RepStore.Chunks:FindFirstChild(x-1 .. ", " .. y+1).Parent = workspace.Chunks
end
if RepStore.Chunks:FindFirstChild(x+1 .. ", " .. y-1) then
RepStore.Chunks:FindFirstChild(x+1 .. ", " .. y-1).Parent = workspace.Chunks
end
if workspace.Chunks:FindFirstChild(x+2 .. ", " .. y+2) then
workspace.Chunks:FindFirstChild(x+2 .. ", " .. y+2).Parent = RepStore.Chunks
end
if workspace.Chunks:FindFirstChild(x+2 .. ", " .. y+1) then
workspace.Chunks:FindFirstChild(x+2 .. ", " .. y+1).Parent = RepStore.Chunks
end
if workspace.Chunks:FindFirstChild(x+2 .. ", " .. y) then
workspace.Chunks:FindFirstChild(x+2 .. ", " .. y).Parent = RepStore.Chunks
end
if workspace.Chunks:FindFirstChild(x+2 .. ", " .. y-1) then
workspace.Chunks:FindFirstChild(x+2 .. ", " .. y-1).Parent = RepStore.Chunks
end
if workspace.Chunks:FindFirstChild(x+2 .. ", " .. y-2) then
workspace.Chunks:FindFirstChild(x+2 .. ", " .. y-2).Parent = RepStore.Chunks
end
if workspace.Chunks:FindFirstChild(x+1 .. ", " .. y-2) then
workspace.Chunks:FindFirstChild(x+1 .. ", " .. y-2).Parent = RepStore.Chunks
end
if workspace.Chunks:FindFirstChild(x .. ", " .. y-2) then
workspace.Chunks:FindFirstChild(x .. ", " .. y-2).Parent = RepStore.Chunks
end
if workspace.Chunks:FindFirstChild(x-1 .. ", " .. y-2) then
workspace.Chunks:FindFirstChild(x-1 .. ", " .. y-2).Parent = RepStore.Chunks
end
if workspace.Chunks:FindFirstChild(x-2 .. ", " .. y-2) then
workspace.Chunks:FindFirstChild(x-2 .. ", " .. y-2).Parent = RepStore.Chunks
end
if workspace.Chunks:FindFirstChild(x-2 .. ", " .. y-1) then
workspace.Chunks:FindFirstChild(x-2 .. ", " .. y-1).Parent = RepStore.Chunks
end
if workspace.Chunks:FindFirstChild(x-2 .. ", " .. y) then
workspace.Chunks:FindFirstChild(x-2 .. ", " .. y).Parent = RepStore.Chunks
end
if workspace.Chunks:FindFirstChild(x-2 .. ", " .. y+1) then
workspace.Chunks:FindFirstChild(x-2 .. ", " .. y+1).Parent = RepStore.Chunks
end
if workspace.Chunks:FindFirstChild(x-2 .. ", " .. y+2) then
workspace.Chunks:FindFirstChild(x-2 .. ", " .. y+2).Parent = RepStore.Chunks
end
if workspace.Chunks:FindFirstChild(x-1 .. ", " .. y+2) then
workspace.Chunks:FindFirstChild(x-1 .. ", " .. y+2).Parent = RepStore.Chunks
end
if workspace.Chunks:FindFirstChild(x .. ", " .. y+2) then
workspace.Chunks:FindFirstChild(x .. ", " .. y+2).Parent = RepStore.Chunks
end
if workspace.Chunks:FindFirstChild(x+1 .. ", " .. y+2) then
workspace.Chunks:FindFirstChild(x+1 .. ", " .. y+2).Parent = RepStore.Chunks
end
end