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

How to put a block to the sides of other blocks?

Asked by 1 year ago

Hello!

I want to make a build tool to build anything. Basically, my tool works well... However, the blocks that I want to put doesn't appear on the sides of other blocks. Exactly, when I hover the mouse, for example, on the side of a pillar, then when I press the left mouse button, the block appears on pillar's top, and not where my mouse is or not on the side of the pillar.

Here's my localscript inside my tool:

--- Services ---
local rs = game:GetService("ReplicatedStorage")

--- Modules ---
local BlockModule = require(rs.BlockModule)
local ImageModule = require(rs.ImageModule)
local WeldModule = require(rs.WeldModule)

--- Variables ---
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

local tool = script.Parent

local function round(number,increment)
    number = math.floor(number)
    number = number-(number%increment)
    return number
end

--- Actual Code ---
tool.Equipped:Connect(function()
    local placement = rs.Blocks:FindFirstChild("Part"):Clone()
    placement.Anchored = true
    placement.Transparency = 0.5
    placement.CanTouch = false
    mouse.TargetFilter = placement

    placementmodel = Instance.new("Model",workspace)
    placementmodel.Name = player.Name.."'s placement"
    placement.Parent = placementmodel
    placementmodel.PrimaryPart = placement

    movePlacement = game:GetService("RunService").Stepped:Connect(function() -- it makes placement of selected block follow player's mouse
        placementmodel:MoveTo(Vector3.new(round(mouse.Hit.Position.X,1),round(mouse.Hit.Position.Y,1),round(mouse.Hit.Position.Z,1)))
    end)

    onMouseClick = mouse.Button1Down:Connect(function()
        rs.Place:FireServer("Part", placementmodel.PrimaryPart.Position)
    end)
end)
tool.Unequipped:Connect(function()
    onMouseClick:Disconnect()
    movePlacement:Disconnect()
    placementmodel:Destroy()
end)

I hope I explained my problem clearly.

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

when you do

movePlacement = game:GetService("RunService").Stepped:Connect(function() -- it makes placement of selected block follow player's mouse
placementmodel:MoveTo(Vector3.new(round(mouse.Hit.Position.X,1),round(mouse.Hit.Position.Y,1),round(mouse.Hit.Position.Z,1)))
end)

instead of

placementmodel:MoveTo(Vector3.new(round(mouse.Hit.Position.X,1),round(mouse.Hit.Position.Y,1),round(mouse.Hit.Position.Z,1)))

do

placementmodel:PivotTo(CFrame.new(Vector3.new(round(mouse.Hit.Position.X,1),round(mouse.Hit.Position.Y,1),round(mouse.Hit.Position.Z,1))))

this is because :MoveTo() uses physics slightly more, as the part you create (likely) has collisions on, it moves on top because it cant fit into the given placement, whereas :PivotTo() ignores physical properties and just moves it instantly, sorry if this isnt that clear though!

lmk if this works!

0
Thanks for the help, now the block can really be located on the sides of other blocks! But now I have the following problem: when the mouse hovers over some block, placement falls into it by half but CanCollide property of placement is true. I think it happened because of my understatement in this topic. Is there any solution to problem that causes placement to penetrate into other blocks by half? IgromanEXE 9 — 1y
Ad

Answer this question