Trying to set a max distance for a build system

Maybe this will work!

if player:DistanceFromCharacter(mouse.Hit.Position) <= 135 then
-- your code here
end
  1. Make a model for player’s mouse to make player’s mouse ignore objects inside the model
  2. Use mouse.TargetFilter = model to make player’s mouse ignore preview build object(s)
  3. Preview build object(s) should be parented to the model

What do you mean by make a model for player’s mouse? Where shall I put the model?

It was easier for me to answer with a link:
Mouse.TargetFilter

I’ve basically added a hitbox and set the mouse’s target filter to that but it keeps doing this:
robloxapp-20210531-1810370.wmv (271.0 KB)

You can try this (read comments):

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

-- Make ignore model for player's mouse and make the mouse ignore everything in the model
Instance.new("Model", workspace).Name = "MouseIgnoreModel"
mouse.TargetFilter = workspace.MouseIgnoreModel

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
					BuildItems[i].Parent = workspace.MouseIgnoreModel -- mouse will ignore build item
				else
					BuildItems[i].BrickColor = BrickColor.new("Really red")
					BuildItems[i].Position = mouse.Hit.Position
					--BuildItems[i].Parent = workspace
					BuildItems[i].Parent = workspace.MouseIgnoreModel -- mouse will ignore build item
				end
			end
		end
	end)
end)

I think that you didn’t do what I meant. I think that this is a better explanation.

If (Position1.Magnitude - Position2.Magnitude) < Range then

@Spiderr12PL Thank you so much! I need one more thing to finish this off. I don’t really know how to turn it back green.

Rather than using a mouse filter, you should ray cast with the said part as a blacklisted instance. The ray should be the camera to the mouse. You can then incorporate distance detection as part of the ray via getting the ray’s unit and multiplying it if that makes any sense

Something is missing in this piece of code (it should be easy to find out what is missing):

if distance <= maxDistance then
BuildItems[i].Position = mouse.Hit.Position
--BuildItems[i].Parent = workspace
BuildItems[i].Parent = workspace.MouseIgnoreModel -- mouse will ignore build item
else
BuildItems[i].BrickColor = BrickColor.new("Really red")
BuildItems[i].Position = mouse.Hit.Position
--BuildItems[i].Parent = workspace
BuildItems[i].Parent = workspace.MouseIgnoreModel -- mouse will ignore build item
end

Forgot to mention that I intentionally didn’t add the missing command.

Maybe it’s… to change it back?

Maybe it’s an elseif statement or something?

Well… I really can’t find the missing piece out…

if distance <= maxDistance then -- Player allowed to build, preview color should be green
BuildItems[i].Position = mouse.Hit.Position
--BuildItems[i].Parent = workspace
BuildItems[i].Parent = workspace.MouseIgnoreModel -- mouse will ignore build item
--Nothing sets the preview's color to green.
else
BuildItems[i].BrickColor = BrickColor.new("Really red") -- this is where the preview is set to red. There is no going back to green in the script.
BuildItems[i].Position = mouse.Hit.Position
--BuildItems[i].Parent = workspace
BuildItems[i].Parent = workspace.MouseIgnoreModel -- mouse will ignore build item
end

OHHHH. Don’t worry I set the preview color to green

But by any chance, do you know how to make it turn back to green once the distance is less than the max distance again?

That’ll be the final thing before I mark you as the soultion

if distance <= maxDistance then
BuildItems[i].BrickColor = BrickColor.new("Lime green") -- It was missing
BuildItems[i].Position = mouse.Hit.Position
BuildItems[i].Parent = workspace.MouseIgnoreModel
else
BuildItems[i].BrickColor = BrickColor.new("Really red")
BuildItems[i].Position = mouse.Hit.Position
BuildItems[i].Parent = workspace.MouseIgnoreModel
end

But by any chance, do you know how to make it turn back to green once the distance is less than the max distance again?

I think that… it was answered.