Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

getting an entire model to snap to an imaginary grid?

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

1 answer

✓ Accepted answer

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.

6 comments

0

how would I make the snap 1 stud preferably?

0

simply making the position whole numbers with math.floor()/math.ceil() will snap by 1 stud. I recently posted an example: https://scriptinghelpers.org/questions/38243/click-to-place-wall-build-preview#40631

0

but how would that work in a model?

0

By using MoveTo() rather than setting position.

0

Well in my updated script I get the same error I fixed my script above to show the current

0

nvm I got it thank you for your help! :)

This discussion is preserved as it was. New answers, comments, and votes are closed.