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.
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.