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

How do I restrict a building distance?

local Player = game.Players.LocalPlayer

local Mouse = Player:GetMouse()

local Distance = 25

  

Mouse.Button1Down:Connect(function(Hit)

local part = Instance.new("Part",workspace)

part.Position = Vector3.new(Mouse.Hit.Position - ((Player.Character.Head.Position - Mouse.Hit.Position).magnitude) + 25)

end)

What I am trying to do is restrict the distance of where you can place a brick to 25 studs. Basically, the brick won't place 25 or more studs away from the player. How would I make this work? Right now, it only spawns under me with no errors.

1 answer

✓ Accepted answer

You're right about having to use magnitude, but you didn't use it like you should. Also, you should be aware that your code does not abide by the rules of Filtering Enabled, so the part will only be seen by the client.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local MaxDistance = 25

Mouse.Button1Down:Connect(function()
	local distance = (Player.Character.Head.Position - Mouse.Hit.p).magnitude
	if distance <= MaxDistance then
		local part = Instance.new("Part")
		part.Parent = workspace --second argument of .new() deprecated
		part.Position = Mouse.Hit.p
	end
end)

Be aware that Button1Down has no parameters. You can, however, access the Hit property through the :GetMouse() method like I did.

1 comment

0

This only places the part if you click 25 studs to the player. I wanted the script to be able to click anywhere, but if you do more than 25 studs, it puts the brick inside a 25 stud area.

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