Trying to make a placement system, but want to snap the parts to a 1x1 stud grid
heres what i have currently
-- Varibles
local Player = game:GetService("Players").LocalPlayer
local RunService = game:GetService("RunService")
local Mouse = Player:GetMouse()
local PlotModule = require(game.ReplicatedStorage.Modules.plotHandler)
local ServerPlaceEvent = game.ReplicatedStorage.Events.PlaceEvent
local PlaceEvent = game.ReplicatedStorage.Events.StartPlace
local PlacementPart = game.ReplicatedStorage.Items.AreaPart:Clone(); PlacementPart.Size = Vector3.new(1,1,1)
local CurrentPart
local v
local Pos
-- Functions
local function RVector(Pos)
return Vector3.new(math.floor(Pos.X), math.floor(Pos.Y), math.floor(Pos.Z))
end
local function MovePart()
local PSize = PlacementPart.Size
Pos = RVector(Mouse.Hit.p) + Vector3.new(0,PSize.Y/2,0)
PlacementPart.Position = Pos
end
local function ReadyPlacing(PartSize)
if CurrentPart and PartSize == CurrentPart then
RunService:UnbindFromRenderStep("MovePart")
PlacementPart.Parent = game.ReplicatedStorage.Items
CurrentPart = nil
return
end
if CurrentPart and PartSize ~= CurrentPart then
RunService:UnbindFromRenderStep("MovePart")
end
CurrentPart = PartSize
if PartSize == "1x1" then
v = Vector3.new(1,1,1)
elseif PartSize == "2x2" then
v = Vector3.new(2,2,2)
end
PlacementPart.Parent = workspace
PlacementPart.Size = v
RunService:BindToRenderStep("MovePart", 1, MovePart)
end
local function PlacePart()
if CurrentPart then
ServerPlaceEvent:FireServer(v, Pos)
end
end
-- Main
-- Event Functions
PlaceEvent.Event:Connect(ReadyPlacing)
Mouse.Button1Down:Connect(PlacePart)
(Edit)
Tried using math.round
local function RVector(Pos)
return Vector3.new(math.round(Pos.X), math.round(Pos.Y), math.round(Pos.Z))
end
didnt really work for part sizes besides 2x2
(Edit 2)
Changed .round to .floor
local function RVector(Pos)
return Vector3.new(math.floor(Pos.X), math.floor(Pos.Y), math.floor(Pos.Z))
end
Still having issues with the parts not snapping onto the grid correctly, not to easy to explain but heres a video