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

The preview of my building tool is flying off to my face?

Asked by 6 years ago
Edited 6 years ago

I'm creating a tool to place blocks. I started by creating a preview of the block. When I finished, I noticed that the preview was a bit buried, so I tried to fix it. But then when I made the changes, the block would fly towards the camera. How do I fix this problem, while making the preview entirely above ground?

------------------
--[DECLARATIONS]--
------------------

----/[SERVICES]\----

----/[MAIN]\----
local plr = game.Players
local tool = script.Parent

----/[VARIABLES]\----
local ghostBlock

----/[CONSTANTS]\----
local CELL_SIZE = 1
local CON_NAME = "[".. plr.Name .."/BuildTool/Local]"

----/[BLOCKS]\----
local woodBlock1 = {
    Size = Vector3.new(4,1,4),
    Color = BrickColor.new("CGA brown"),
    Material = "WoodPlanks",
    Transparency = 0,
    Reflectance = 0,

}

--------------------
--[INITIALIZATION]--
--------------------

---------------
--[FUNCTIONS]--
---------------

--Snaps the position to the grid
function SnapPosToGrid(rawPos, size)
    --Gets the raw position and snaps it to the grid defined by the cell size
    local snappedPos = CFrame.new(  rawPos.X - rawPos.X % CELL_SIZE,
                                    (rawPos.Y - rawPos.Y % CELL_SIZE) + size.Y/2,
                                    rawPos.Z - rawPos.Z % CELL_SIZE)

    --Return the position
    return snappedPos
end

--Create ghost
function CreateGhostBlock(block)
    --Create part and customize it
    local ghost = Instance.new("Part",workspace)
        ghost.Size = block.Size
        ghost.BrickColor = block.Color
        ghost.Material = block.Material
        ghost.Transparency = .5
        ghost.Anchored = true
        ghost.CanCollide = false
        ghost.Locked = true
        ghost.Name = "Ghost"

    --Return the created part
    return ghost
end

--The mouse has moved, create a ghost block
function MouseMoved(mouse)
    --If the target of the mouse is not permited, don't do anything
    --if mouse.Target.Name == "Ghost" then return end   

    --Get the position of the mouse and snap it to the grid
    local mouseHit = mouse.Hit.p
    local gridCFrame = SnapPosToGrid(mouseHit,woodBlock1.Size)

    --Create a ghost part if it doesn't exist and change the position of it
    if not ghostBlock then
        ghostBlock = CreateGhostBlock(woodBlock1)
    end 
    ghostBlock.CFrame = gridCFrame
end

------------------
--[SCRIPT LOGIC]--
------------------

--The tool has been equipped
tool.Equipped:Connect(function(mouse)
    --Mouse has not been found for some reason
    if not mouse then warn(CON_NAME..": Mouse has not been found!") return end

    local mouseMoved = mouse.Move:Connect(function() MouseMoved(mouse) end)

    --Tool has been unequipped
    tool.Unequipped:Connect(function()
        --Disconnect Events
        if mouseMoved then mouseMoved:Disconnect() end
    end)
end)

1 answer

Log in to vote
0
Answered by 6 years ago

Ok, I found it out. You need to add the ghost part to the mouse filter.

--Create a ghost part if it doesn't exist and change the position of it
if not ghostBlock then
    ghostBlock = CreateGhostBlock(woodBlock1)
    mouse.TargetFilter = ghostBlock
end 
Ad

Answer this question