So I’ve noticed that some people code like this
local template =
ReplicatedStorage.Assets.Skins:FindFirstChild(player:GetAttribute "ActiveSkin" or "Default" )
or ReplicatedStorage.Assets.Platform
Rather than just leaving it on one line. Is this needed or just for readability?
Whole script (Lazy coding)
local PlatformLayout = {}
PlatformLayout.__index = PlatformLayout
local Players = game:GetService "Players"
local ReplicatedStorage = game:GetService "ReplicatedStorage"
function PlatformLayout.new(folder, spacing, anchor)
local self = setmetatable({}, PlatformLayout)
self.Folder = folder or workspace:FindFirstChild "Platforms" or Instance.new("Folder", workspace)
self.Folder.Name = "Platforms"
self.Gap = spacing or 80
local anchorModel = anchor or ReplicatedStorage.Assets.Skins:FindFirstChild "Default"
if anchorModel then
self.Origin = anchorModel:GetPivot().Position
else
self.Origin = Vector3.zero
end
return self
end
function PlatformLayout:Assign()
local players = {}
for _, player in ipairs(Players:GetPlayers()) do
local inMainMenu = player:GetAttribute "InMenu"
local afk = player:GetAttribute "AFK"
if not inMainMenu and not afk then
table.insert(players, player)
end
end
for i, player in ipairs(players) do
local platform = self.Folder:FindFirstChild(tostring(i))
if platform then
local config = platform:FindFirstChild "Config"
if config then
config:SetAttribute("Player", player.Name)
end
task.spawn(function()
repeat task.wait() until player.Character
player.Character:PivotTo(platform:GetPivot() * CFrame.new(0, 25, 0))
end)
end
end
end
function PlatformLayout:Arrange()
local existingPlatforms = self.Folder:GetChildren()
if #existingPlatforms == 0 then
local activePlayers = {}
for _, player in ipairs(Players:GetPlayers()) do
local inMainMenu = player:GetAttribute "InMenu"
local afk = player:GetAttribute "AFK"
if not inMainMenu and not afk then
table.insert(activePlayers, player)
end
end
for index, player in ipairs(activePlayers) do
local template = ReplicatedStorage.Assets.Skins:FindFirstChild(player:GetAttribute "ActiveSkin" or "Default" ) or ReplicatedStorage.Assets.Platform
local newPlatform = template:Clone()
newPlatform.Name = tostring(index)
newPlatform.Parent = self.Folder
end
end
local platforms = self.Folder:GetChildren()
table.sort(platforms, function(a, b)
return tonumber(a.Name) < tonumber(b.Name)
end)
local count = #platforms
local cols = math.ceil(math.sqrt(count))
local totalWidth = (cols - 1) * self.Gap
local totalDepth = (math.ceil(count / cols) - 1) * self.Gap
local originX = self.Origin.X - totalWidth / 2
local originZ = self.Origin.Z - totalDepth / 2
local originY = self.Origin.Y
for i, platform in ipairs(platforms) do
local col = (i - 1) % cols
local row = math.floor((i - 1) / cols)
local position = Vector3.new(originX + col * self.Gap, originY, originZ + row * self.Gap)
platform:PivotTo(CFrame.new(position))
end
self:Assign()
end
function PlatformLayout:Remove()
for _, platform in ipairs(self.Folder:GetChildren()) do
platform:Destroy()
end
end
return PlatformLayout