Mouse.Hit limitation not working

Hi, I am making a placement system but I have encountered an issue, so I want to calculate the distance between the mouse.Hit.Position and the local players humanoid root part, I tried fixing it via different ways but no luck, heres the code

local PosX, PosY, PosZ
local Grid = 1
local player = game.Players.LocalPlayer
local screengui = player.PlayerGui:WaitForChild("ScreenGui")
local mouse = player:GetMouse()
local UIS = game:GetService("UserInputService")
local Part = game.ReplicatedStorage.Part:Clone()
Part.Name = "GhostPart"
for i,v in pairs(Part:GetChildren()) do
	v.CanCollide = false
end
local orien
local function Snap(posy)
	PosX = math.floor(mouse.Hit.X / Grid + 0.5) * Grid -- Snaps
	PosY = 1
	PosZ = math.floor(mouse.Hit.Z / Grid + 0.5) * Grid -- Snaps
end
local toggle = false
local function Move()
	if toggle == true then
		
	
	mouse.TargetFilter = Part
	Snap()
	Part:SetPrimaryPartCFrame(CFrame.new(PosX, mouse.Hit.p.Y + 3, PosZ))
	if orien ~= nil then
		Part.PrimaryPart.Orientation = orien
		end
		end
end
local function rotate()
	Part.PrimaryPart.Orientation += Vector3.new(0,10,0)
	orien = Part.PrimaryPart.Orientation
end
local function toggleb()
	if toggle == true then
		Part.Parent = game.ReplicatedStorage
		screengui.Captions2.Visible = false
		toggle = false
	else
		Part.Parent = game.Workspace
		toggle = true
		screengui.Captions2.Visible = true
	end
end
mouse.Move:Connect(Move)
UIS.InputBegan:Connect(function(input,gamep)
	if not gamep then
		if input.KeyCode == Enum.KeyCode.R then
			rotate()
		elseif input.KeyCode == Enum.KeyCode.B then
			toggleb()
		end
	end
end)
local deb = true
mouse.Button1Down:Connect(function()
	if toggle == true then
		for i,v in pairs(game.Players:GetPlayers()) do
			if v.Character then
				if (Part.PrimaryPart.Position - v.Character.HumanoidRootPart.Position).magnitude > 10 then
					if (Part.PrimaryPart.Position - player.Character.HumanoidRootPart.Position).magnitude > 15 then
						game.ReplicatedStorage.PlaceEvent:FireServer(mouse.Hit.Position,orien)
					else
						if deb then
							deb = false
							screengui.Captions.Text = "Error, can not place Cabinet due to far away distance"
							screengui.Captions.Visible = true
							wait(2)
							screengui.Captions.Visible = false
							deb = true
						end
					end
				else
					
					if deb then
						deb = false
						screengui.Captions.Text = "Error, can not place Cabinet due to a player nearby, player: " .. v.Name
						screengui.Captions.Visible = true
						wait(2)
						screengui.Captions.Visible = false
						deb = true
					end
					
					
				end
			end
		end
		
		end
end)

I don’t understand the issue exactly but maybe this is what you wanted?

local Dist = (Root.Position - mouse.Hit.Position).Magnitude

I tried doing that but it did not work, I got the magnitude of the distance between the root part and the hit position and checked if it doesnt exceed 15 studs, but it doesnt work, sorry I may have had some mistake in my sentence up there.

I wouldn’t recommend using mouse.hit since Roblox actually prefers you not using the mouse object at all

I see, any alternatives to use you may recommend?

You can get a ray from the mouse position using user input service and the camera

I will try that and let you know. Thanks

Maybe could you use raycast, raycasting the mouse position and the root position.
This video explain more:

2 Likes

I think your issue is that you need to do less than or equal to (<=) because you want it to only place if it’s within 15 studs

I tried that and it worked, Thank you very much!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.