hey basically im working on a fortnite building system
but i have a problem which is the incrementing of the grid placement system
local self = setmetatable({}, {__index = {}})
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local function CurrentModel(model)
local cloned = model:Clone()
cloned.Parent = workspace
cloned.Hitbox.Transparency = .75
return cloned
end
local RunService = game:GetService("RunService")
local part = CurrentModel(game.ReplicatedStorage["Project Royale Build Models"].Wall)
Mouse.TargetFilter = part
local RoundTo = 18
function grid(x)
return math.floor((x / RoundTo) + 0.5) * RoundTo
end
function NotColliding(part)
local region = Region3.new(
part.PrimaryPart.Position - (part.PrimaryPart.Size * 0.45),
part.PrimaryPart.Position + (part.PrimaryPart.Size * 0.45)
)
local tab = workspace:FindPartsInRegion3WithIgnoreList(region, {workspace.Baseplate,workspace.Terrain, unpack(part:GetChildren())})
if #tab == 0 then
return true
else
return false
end
end
function Place(part)
if not NotColliding(part) then return end
local Clone = part:Clone()
Clone.Hitbox.BrickColor = BrickColor.new("Linen")
Clone.Parent = workspace
Clone.Hitbox.Transparency = 0
Clone.Hitbox.CanCollide = true
end
Mouse.Button1Down:Connect(function()
Place(part)
end)
game:GetService("UserInputService").InputBegan:Connect(function(Input, gp)
if Input.KeyCode == Enum.KeyCode.Q then
part:Destroy()
part = CurrentModel(game.ReplicatedStorage["Project Royale Build Models"].Wall)
elseif Input.KeyCode == Enum.KeyCode.E then
part:Destroy()
part = CurrentModel(game.ReplicatedStorage["Project Royale Build Models"].Floor)
elseif Input.KeyCode == Enum.KeyCode.Z then
part:Destroy()
part = CurrentModel(game.ReplicatedStorage["Project Royale Build Models"].Ramp)
end
end)
RunService.RenderStepped:Connect(function()
local targetPosition = Mouse.Hit.Position + Vector3.new(0, part.PrimaryPart.Size.Y * 0.5, 0)
local X = grid(targetPosition.X)
local Y = grid(targetPosition.Y)
local Z = grid(targetPosition.Z)
local IsColliding = not NotColliding(part)
if IsColliding then
part.Hitbox.BrickColor = BrickColor.new("Really red")
else
part.Hitbox.BrickColor = BrickColor.new("Linen")
end
local lookVec = Player.Character:GetPrimaryPartCFrame().LookVector
local a = math.atan2(-lookVec.X, -lookVec.Z)
part.PrimaryPart.CFrame = Player.Character:GetPrimaryPartCFrame() * CFrame.new(0, 0, -5) * CFrame.Angles(0, math.rad(math.floor((math.deg(a) / 90) + 0.5) * 90), 0)
part.PrimaryPart.CFrame = CFrame.new(X,Y,Z, part.PrimaryPart.CFrame.RightVector.X,part.PrimaryPart.CFrame.RightVector.Y,part.PrimaryPart.CFrame.RightVector.Z,part.PrimaryPart.CFrame.UpVector.X,part.PrimaryPart.CFrame.UpVector.Y,part.PrimaryPart.CFrame.UpVector.Z,part.PrimaryPart.CFrame.LookVector.X,part.PrimaryPart.CFrame.LookVector.Y,part.PrimaryPart.CFrame.LookVector.Z)
part.PrimaryPart.Orientation = Vector3.new(0,(math.floor((math.deg(a) / 90) + 0.5) * 90),0)
end)
I have bad understanding of math and if u can fix this issue plz do so.
