So I'm creating a game which utilizes a placement system I made, and I would like to create a grid on the client to outline where the user can place things. The following code does just that, but I would like to make it more efficient because I feel as though theres a lot of redundant math (and code, namely lines 12 and 13).
You might notice that instead of creating longer lines which are the length of the plot, I opted to create many smaller lines which are the length of each tile; this is intentional and essential for other parts of the system to function properly.
Here's a picture of what the grid looks like http://gyazo.com/2b9600...
function Module.CreateGrid(GridSize)
if game:GetService("RunService"):IsClient() then
local Plot = Module.GetPlot(game.Players.LocalPlayer)
if Plot then
local Grid = Instance.new("Folder")
for x = Plot.Size.X / -2, Plot.Size.X / 2 - GridSize, GridSize do
for z = Plot.Size.Z / -2, Plot.Size.Z / 2 - GridSize, GridSize do
CreatePart("Square", Grid, GridSize, CFrame.new(x + GridSize / 2, 0.1, z + GridSize / 2) * Plot.CFrame, CFrame.Angles(0, 0, 0))
CreatePart("Line", Grid, GridSize, CFrame.new(x, 0.1, z + GridSize / 2) * Plot.CFrame, CFrame.Angles(0, math.rad(90), 0))
CreatePart("Line", Grid, GridSize, CFrame.new(x + GridSize / 2, 0.1, z) * Plot.CFrame, CFrame.Angles(0, 0, 0))
end
CreatePart("Line", Grid, GridSize, CFrame.new(Plot.Size.X / 2, 0.1, x + GridSize / 2) * Plot.CFrame, CFrame.Angles(0, math.rad(90), 0))
CreatePart("Line", Grid, GridSize, CFrame.new(x + GridSize / 2, 0.1, Plot.Size.Z / 2) * Plot.CFrame, CFrame.Angles(0, 0, 0))
end
Grid.Name = "Grid"
Grid.Parent = Plot
end
end
end
function CreatePart(Type, Grid, GridSize, Position, LookAt)
local Part = Instance.new("Part")
Part.Size = Vector3.new(GridSize, 0.1, (Type == "Line") and 0.25 or GridSize)
Part.CFrame = Position * LookAt
Part.BrickColor = (Type == "Line") and BrickColor.White() or BrickColor.Red()
Part.Material = Enum.Material.Neon
Part.Transparency = 0.75
Part.CanCollide = false
Part.Anchored = true
Part.Name = Type
Part.Parent = Grid
end

2 comments
Yeah im having a similar problem. But i didn't made a grid sistem cuz im rly bad at scripting :P
As long as your code is working it looks fine and since it only needs to run once (I'm assuming) there's no need to optimize it. Math is not expensive, it's doing it many times that is.