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)