Hey,
Basically, I make a wall using a matrix and then create a primary part in the middle and weld everything together with that primary part:
-- Need to reset the matrix when respawned.
local ReplicatedStorage = game:GetService "ReplicatedStorage"
local Component = require(ReplicatedStorage.Packages.Component)
local Assets = ReplicatedStorage:WaitForChild("Assets")
local Wall = Component.new {
Tag = "Wall"
}
function Wall:Construct()
self.Rows = 2
self.Columns = 12
self.Sparsity = .7
self.CenterPosition = nil
self.Matrix = {}
end
function Wall:GenerateRandom(): number
return math.random() > self.Sparsity and 1 or 0
end
function Wall:GenerateMatrix()
for I = 1, self.Rows do
self.Matrix[I] = {}
for J = 1, self.Columns do
self.Matrix[I][J] = self:GenerateRandom()
end
end
end
function Wall:Start()
self:GenerateMatrix()
for I = 1, self.Rows do
for J = 1, self.Columns do
if self.Matrix[I][J] == 1 then
local CrackedCrate = Assets.CrackedCrate:Clone()
CrackedCrate.Position = Vector3.new((J - 1) * CrackedCrate.Size.X, (I - 1) * CrackedCrate.Size.Y, 0)
CrackedCrate.Parent = self.Instance
else
local Crate = Assets.Crate:Clone()
Crate.Position = Vector3.new((J - 1) * Crate.Size.X, (I - 1) * Crate.Size.Y, 0)
Crate.Parent = self.Instance
end
end
end
self.CenterPosition = self.Instance:GetBoundingBox()
local PrimaryPart = Instance.new("Part")
PrimaryPart.Transparency = 1
PrimaryPart.Anchored = true
PrimaryPart.CanCollide = false
PrimaryPart.CFrame = self.CenterPosition
PrimaryPart.Parent = self.Instance
self.Instance.PrimaryPart = PrimaryPart
for _, Crate in self.Instance:GetChildren() do
if Crate.Name ~= "Part" then
local Weld = Instance.new("WeldConstraint")
Weld.Part0 = PrimaryPart
Weld.Part1 = Crate
Weld.Parent = PrimaryPart
end
end
end
function Wall:Stop()
self.Matrix = {}
end
return Wall
In my round module, I pick a random thing from a folder to be spawned in (e.g. the wall). However, for some reason it says that the primary part is nil? Why would it be nil, I have no idea. Thus, my odd situation.
local ReplicatedStorage = game:GetService "ReplicatedStorage"
local Knit = require(ReplicatedStorage.Packages.Knit)
local Trove = require(ReplicatedStorage.Packages.Trove)
local Templates = ReplicatedStorage:WaitForChild("Templates"):GetChildren()
local RoundService = Knit.CreateService {
Name = "RoundService",
_Trove = Trove.new(),
}
function RoundService:_RandomObstacle()
return Templates[math.random(1, #Templates)]
end
function RoundService:StartRound()
local Object = self:_RandomObstacle():Clone()
print(Object.PrimaryPart) -- nil???
Object.Parent = workspace
self._Trove:Add(Object)
end
function RoundService:EndRound()
self._Trove:Clean()
end
function RoundService:KnitStart()
self:StartRound()
end
return RoundService
Any help is help, thank you.