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

I need help adding the code to place blocks wherever I click my mouse?

Asked by 3 years ago

Hello, I am making a building game.I already have the pickup block script but I can't build with it. Can anyone help me and add the script to place the block down to anywhere I click my mouse? thank you

Here is the code I have

local tool = script.Parent.Parent local click = script.Parent.ClickDetector

local function pickup(player) tool.Parent = game.Workspace [player.name ]

end

click.MouseClick: Connect (pickup)

0
No attempt made to actually translate the position to 3D space? DeceptiveCaster 3761 — 3y
0
the reason is because i am very new to coding topo8986 0 — 3y
0
"Thank you but how do I place this code can you place a picture please. I have tried and it does not work. But thank you for the help" Mister33j 86 — 3y
0
i think u put it in serverscriptstorage, basic script Mister33j 86 — 3y
0
I have a question when I click the mouse does it just place down anywhere or does it need a base? topo8986 0 — 3y

1 answer

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

I think it's this:

local function round(vector,grid) 
    return Vector3.new( 
        math.floor(vector.X/grid+.5)*grid, 
        math.floor(vector.Y/grid+.5)*grid, 
        math.floor(vector.Z/grid+.5)*grid 
    ) 
end 

round(mouse.Hit.Position,3)

or:

-- services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")

local player = Players.LocalPlayer
local block = ReplicatedStorage:WaitForChild("block")

-- how far you want the player to reach
local REACH = 100 

mouse.Button2Down:Connect(function()

    local unitRay = mouse.UnitRay

    local castParams = RaycastParams.new()
    castParams.FilterDescendantsInstances = { player.Character }

    -- automatically ignores the player's character

    local result = Workspace:Raycast(
        unitRay.Origin, unitRay.Direction * REACH,
        castParams
    )

    if result then
        local newBlock = block:Clone()
        newBlock.Position = round(result.Position + result.Normal * 1.5, 3)
        newBlock.Parent = workspace
    end

end)

I'm not really good at scripting.

0
Thank you but how do I place this code can you place a picture please. I have tried and it does not work. But thank you for the help topo8986 0 — 3y
0
can you please respond? topo8986 0 — 3y
Ad

Answer this question