So I'm creating a furniture system for my game but I need the entire model to snap to a grid, I know how to snap just a part to a grid and I tried using that function and a for loop to snap each part to a grid in the model but that made it worse
local tool = script.Parent local moveEvent local model = workspace.Chair function Snap(model) local Pos=model:GetModelCFrame().p model:MoveTo(Vector3.new(math.floor(Pos.x),math.floor(Pos.y),math.floor(Pos.z))) end tool.Equipped:connect(function(eventMouse) eventMouse.TargetFilter = model moveEvent = eventMouse.Move:connect(function() -- model:MoveTo(eventMouse.Hit.p) Snap(model) end) end) tool.Unequipped:connect(function() if moveEvent then moveEvent:disconnect() end end) function onKeyPress(actionName, userInputState, inputObject) if userInputState == Enum.UserInputState.Begin then rotateMod(model, CFrame.Angles(0,math.rad(90),0)) end end game.ContextActionService:BindAction("keyPress", onKeyPress, false, Enum.KeyCode.R) function rotateMod(mod,rotation) local center = mod:GetModelCFrame() local parts ={} local function scan(parent) for _,obj in pairs(parent:GetChildren()) do if (obj:IsA("BasePart")) then table.insert(parts,obj) end scan(obj) end end scan(mod) for _,part in pairs(parts) do part.CFrame = (center*rotation*(center:inverse()*part.CFrame)) end end
17:04:14.698 - Unable to cast double to Vector3 17:04:14.699 - Stack Begin 17:04:14.701 - Script 'Players.Player1.Backpack.Place Chair.Placement', Line 6 - global Snap 17:04:14.701 - Script 'Players.Player1.Backpack.Place Chair.Placement', Line 15 17:04:14.702 - Stack End
First off your Snap function is kind of arbitrary. You set the model position to mouse.Hit.p, then snap-to-grid using its new position. You could simply snap-to-grid using mouse.hit.p.
The big issue is your MoveTo. math.min() returns the lowest number in a list of numbers. It won't change a single argument. You want to use math.floor(). Also MoveTo requires a Vector3 argument, but you accidentally gave it 3 numbers. Happens.
This scripts looks like it will work though and I hope it is a learning experience.