Trying to set a max distance for a build system

So, I am trying to create a build system which includes a max distance. I want a max distance from the player’s mouse to the humanoid root part.
So I was trying to test it with using the print function.


This is all the solutions I’ve tried and I’d really be pleased if you have feedback to give me on this.

I apologize if this is a stupid question or you cannot understand this, I’m new to scripting and the dev forums.

1 Like

DistanceFromCharacter is a function of Player. The character is just a model.

I don’t understand the usage of the for loop in this script. This is what I mean:

for i = 2, 2 do
print(i) --The loop would run only once at it would print once value 2 and then stop
end

I would only do something like this for testing purposes, not for final product (this is not very likely).
Do you have any issues with the script? Does the script produce errors?

Try using the Character’s Torso position and Magnitude:

local distance = (player.Character.Torso.Position - mouse.Hit.p).Magnitude

print (distance)

Also, to prevent exploiters building far away, you should use a RemoteEvent and do all the distance checks serverside.

Player’s character moving in any directions also moves camera - this is how player can change mouse.Hit property without firing mouse.Move event.
Let’s assume that you want to use blocks building system. A player moves his mouse. Block spawn location changes. Later the player moves his character forward without moving his mouse. Block spawn location is still in the same location.

So I don’t want you to comment on the rest of the code. I only want you to tell me what I should implement for distances from the mouse to the player and a max distance. Thanks.

This might help you:

local player = game:GetService("Players").LocalPlayer
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:wait()
end
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local maxDistance = 32

while wait() do
local mouseHit = mouse.Hit
local mouseHitPosition = Vector3.new(mouseHit.X, mouseHit.Y, mouseHit.Z)
local rootPartPosition = humanoidRootPart.Position
local distance = (mouseHitPosition - rootPartPosition).Magnitude
print(distance)
if distance <= maxDistance then
-- Do building stuff
-- else do nothing
end
end
1 Like

Next morning I’ll try use this. Thanks a trillion!

I’ll use both of your ideas to create this.

Thanks for also telling me how to prevent exploiters, it’ll be really useful!

One question, is the max distance measured in studs?

It almost worked but after I got it to 32 studs, it’s now red forever. It’s also doing this zooming glitch. I’ve re-written the code so that could possibly be the issue? I’ll give you the video:robloxapp-20210531-0829230.wmv (3.6 MB)
I’ll also give the code:
local BuildItems = {
game.ReplicatedStorage.BuildItems.SelectFrontRoof:Clone(),
game.ReplicatedStorage.BuildItems.SelectWalls:Clone()
}

local player = game:GetService(“Players”).LocalPlayer
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:wait()
end
local humanoidRootPart = character:WaitForChild(“HumanoidRootPart”)
local mouse = player:GetMouse()
local maxDistance = 32

script.Parent.MouseButton1Click:Connect(function()
mouse.Move:Connect(function()
for i = 1, 1 do
while wait() do
local mouseHit = mouse.Hit
local mouseHitPosition = Vector3.new(mouseHit.X, mouseHit.Y, mouseHit.Z)
local rootPartPosition = humanoidRootPart.Position
local distance = (mouseHitPosition - rootPartPosition).Magnitude
print(distance)
if distance <= maxDistance then
BuildItems[i].Position = mouse.Hit.Position
BuildItems[i].Parent = workspace
else
BuildItems[i].BrickColor = BrickColor.new(“Really red”)
BuildItems[i].Position = mouse.Hit.Position
BuildItems[i].Parent = workspace
end
end
end
end)
end)

I would fire the server with the mouse position, then calculate the distance from the player and the mouse on the server, and then check if the distance is smaller than the range you have given because if you do all of that on the client it’s too easy to exploit.

I’ll do it by a remote event, is that fine?

How do you do it by remote event.??

Okay, so you send the mouse position over to the server as you said via a remote event and then check the distance between the player and his mouse position like this :
LocalScript:
local Mouse = Player:GetMouse()

then you fire the remote event like if the player clicks or something idk.
remote:FireServer(Mouse.Hit)

ServerScript:
local Range = 150
remote.OnServerEvent:Connect(function(Player, Mouse)
local Distance = (Mouse.Position - HumanoidRP.Position).Magnitude
if Distance < Range then
– do stuff
end
end)

Thanks! I’ll try the code. Hope it works!

No problem if it does not work like you wanted it to then we can make it work like you want it to;)

Thanks! It worked but now I need a fix for this bug. It keeps doing this weird glitch where it zooms out and I also need it to turn back green once the distance is not equal to or more than the max distance. I’ll show you what happens when I play:
robloxapp-20210531-0829095.wmv (497.6 KB)

If I don’t get a reply by tomorrow or later, I’ll have to do a post about this bug…