Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Placement system should snap to grid how do I do that?

Asked by 6 years ago
01wait ()
02script.Parent.Button.MouseButton1Click:Connect(function()
03    local Object = "Test"
04    local Mouse = game.Players.LocalPlayer:GetMouse()
05    local Part = game.Workspace.MainSystem.Test:Clone()
06    Part.Parent = game.Workspace.MainSystem
07    wait ()
08    while true do
09        wait ()
10        Part.Position = Vector3.new(Mouse.Hit.Position.X, 1.5, Mouse.Hit.Position.Z)
11    end
12end)

This script places the part directly at the position of the mouse but how do I do that so that it only ever moves by 1 stud?

0
Round it to the nearest stud, egomoose actually made a really good tutorial on this recently callled creating a furniture placement system turtle2004 167 — 6y
0
Also, i would use runservice instead of a while loop turtle2004 167 — 6y

1 answer

Log in to vote
2
Answered by
zblox164 531 Moderation Voter
6 years ago
Edited 6 years ago

Fixing bugs

First off Mouse.Hit.Position.X is incorrect. You should write it like this: Mouse.Hit.X this will return the x axis of the mouse position. You need to do this for the z as well. If you want to return the position on all axis use Mouse.Hit.p.

Snapping to grid

You can divide the mouse position by your grid size and round it up or down and then multiply it by grid size to snap to a grid. For rounding you can use math.floor() and math.ceil(). Floor rounds down and ceil rounds up. In this case it does not matter which one you use. I will use floor.

Example:

01local mouse = game.Players.LocalPlayer:GetMouse()
02 
03local posX
04local posY
05local posZ
06 
07local gridSize = 1
08 
09local function Snap()
10    posX = math.floor(mouse.Hit.X / gridSize + 0.5) * gridSize
11    posY = mouse.Hit.Y -- or what ever you want
12    posZ = math.floor(mouse.Hit.Z / gridSize + 0.5) * gridSize
13end
14 
15mouse.Move:Connect(function()
16    Snap()
17 
18    Part.Position = Vector3.new(posX, posY, posZ)
19end

Since you are snapping to a 1x1x1 grid you only have to round down (or up). I just showed you the above because in the future you may want a bigger grid. Here's the simpler way:

01local mouse = game.Players.LocalPlayer:GetMouse()
02 
03local posX
04local posY
05local posZ
06 
07local function Snap()
08    posX = math.floor(mouse.Hit.X)
09    posY = mouse.Hit.Y -- or what ever you want
10    posZ = math.floor(mouse.Hit.Z)
11end
12 
13mouse.Move:Connect(function()
14    Snap()
15 
16    Part.Position = Vector3.new(posX, posY, posZ)
17end

One other thing is you may not want to move the mouse using the Moveevent. Instead use RunService.

End

The only down side to this way is you cannot add studs to it.

Hope this helps!

Ad

Answer this question