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

How to make the placement, place in grid?

I have a grid for placing furniture and each grid square is 2 studs by 2 studs how can I make it so when you place it stays in the 2 by 2 grid lines. This is what I have so far:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local mTarget
local Down

function clickObject()
	if Player.BuildMode.Value == true and Mouse.Target ~= nil and not game.Workspace.StarterPlot then
		mTarget = Mouse.Target
		print(mTarget)
		Mouse.TargetFilter = mTarget
		print(Mouse.TargetFilter)
		Down = true
	end
end


Mouse.Button1Down:Connect(clickObject)


function mouseMove()
	if Down and mTarget then
		local posX, posY, posZ = Mouse.Hit.X, Mouse.Hit.Y, Mouse.Hit.Z
		posX = posX/2
		local function round(posX)
			return math.floor(posX + 0.5)
		end
		posX = posX * 2
		posY = posY/2
		local function round(posY)
			return math.floor(posY + 0.5)
		end
		posY = posY * 2
		posZ = posZ/2
		local function round(posZ)
			return math.floor(posZ + 0.5)
		end
		posZ = posZ * 2
		mTarget.Position = Vector3.new(posX, posY, posZ)
	end
end

Mouse.Move:Connect(mouseMove)

function mouseDown()
	Down = false
	Mouse.Target = nil
	Mouse.TargetFilter = nil
end
Mouse.Button1Up:Connect(mouseDown)

1 answer

✓ Accepted answer

Divide the Mouse X Value by 2(Your Grid Size) then round that number and then finally multiply by 2(Your Grid Size). Then you can only get a value that is a multiple of 2(Your grid size).

Do this for the X, Y and the Z

Hope this works

Sorry for all the confusion, heres a modified version of your script that'll work

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local mTarget
local Down

function clickObject()
    if Player.BuildMode.Value == true and Mouse.Target ~= nil and not game.Workspace.StarterPlot then
        mTarget = Mouse.Target
        print(mTarget)
        Mouse.TargetFilter = mTarget
        print(Mouse.TargetFilter)
        Down = true
    end
end


Mouse.Button1Down:Connect(clickObject)

local function round(number)
            return math.floor(number + 0.5)
end

function mouseMove()
    if Down and mTarget then
        local posX, posY, posZ = Mouse.Hit.X, Mouse.Hit.Y, Mouse.Hit.Z
        posX = posX/2
        posX = round(posX) * 2
        posY = posY/2
        posY = round(posY) * 2
        posZ = posZ/2
        posZ = round(posZ) * 2
        mTarget.Position = Vector3.new(posX, posY, posZ)
    end
end

Mouse.Move:Connect(mouseMove)

function mouseDown()
    Down = false
    Mouse.Target = nil
    Mouse.TargetFilter = nil
end
Mouse.Button1Up:Connect(mouseDown)

5 comments

0

How would I round the number through the script?

0

You can use the function: local function round(n) return math.floor(n + 0.5) end just run the function as round(the number you want to round to nearest whole)

0

It didn't work I edited the post so here's the script please tell me what is wrong. Thanks

0

It still doesn't work..... I've searched but still it does not work

0

If it isn't quite working as intended, try changing the "0.5" to "0" on line 20. example: return math.floor(number + 0). This would fix the problem of the block being half offseted.

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