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

How do I restrict a building distance?

Asked by 5 years ago
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

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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.

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. Thesquid13 301 — 5y
Ad

Answer this question