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

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

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)

5 comments

0

No attempt made to actually translate the position to 3D space?

0

the reason is because i am very new to coding

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"

0

i think u put it in serverscriptstorage, basic script

0

I have a question when I click the mouse does it just place down anywhere or does it need a base?

1 answer

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.

2 comments

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

0

can you please respond?

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