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

How to make the placement, place in grid?

Asked by 3 years ago
Edited 3 years ago

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

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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)
0
How would I round the number through the script? TestUser4646 1 — 3y
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) UltimateGameDudez 19 — 3y
0
It didn't work I edited the post so here's the script please tell me what is wrong. Thanks TestUser4646 1 — 3y
0
It still doesn't work..... I've searched but still it does not work TestUser4646 1 — 3y
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. UltimateGameDudez 19 — 3y
Ad

Answer this question