Is there a way to get an instance from just mouse.Hit?

I am creating a building game and I need to add a selection box to an object that you are about to delete, the problem is I’m using mouse.Hit and that doesn’t include the instance. Sure i could use raycasting but i have tried it but the DevHub’s Article on Raycasting has nothing to do with mouse so I just used

local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:RayCast(game.Players.LocalPlayer.Character.Head.Position, mouse.Hit.Position, raycastParams)
local Box = Instance.new("SelectionBox")
				Box.Color3 = Color3.fromRGB(255, 0, 4)
				Box.SurfaceColor3 = Color3.fromRGB(172, 0, 2)
				Box.LineThickness = 0.15
				Box.SurfaceTransparency = 0.7
				Box.Transparency = 0
				Box.Parent = raycastResult.Instance
				Box.Adornee = raycastResult.Instance
				game:GetService("RunService").Heartbeat:Wait()
				Box:Destroy()

and this script did not work, so is there a way to just get an instance from mouse.Hit so i can cut this down?

Full Script
local mouse = game.Players.LocalPlayer:GetMouse()
local selectionEnabled = false

game:GetService("RunService").RenderStepped:Connect(function()
	if game.ReplicatedStorage.Deleting.Value == false then
		workspace.Select.Transparency = 0.5
		workspace.Select.Position = Vector3.new(math.round(mouse.Hit.Position.X) + game.ReplicatedStorage.Offsets.X.Value, math.round(mouse.Hit.Position.Y) + game.ReplicatedStorage.Offsets.Y.Value, math.round(mouse.Hit.Position.Z) + game.ReplicatedStorage.Offsets.Z.Value)
	else -- start of problem
			local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local raycastResult = workspace:Raycast(mouse.Hit.p, mouse.Hit.p,raycastParams)
		if raycastResult then
				if raycastResult.Instance.Locked == false and raycastResult.Instance:FindFirstChild("DeleteBox") then
				workspace.Select.Transparency = 1
				local Box = Instance.new("SelectionBox")
				Box.Color3 = Color3.fromRGB(255, 0, 4)
				Box.SurfaceColor3 = Color3.fromRGB(172, 0, 2)
				Box.LineThickness = 0.15
				Box.SurfaceTransparency = 0.7
				Box.Transparency = 0
				Box.Parent = raycastResult.Instance
				Box.Adornee = raycastResult.Instance
				game:GetService("RunService").Heartbeat:Wait()
				Box:Destroy()
			end
		end
	end
end)
--end of problem
mouse.Button1Down:Connect(function()
	local clone = game.ReplicatedStorage.Part:Clone()
	clone.Parent = workspace
	clone.Position = workspace.Select.Position
	clone.Size = workspace.Select.Size
end)

https://developer.roblox.com/en-us/api-reference/property/Mouse/Target

3 Likes

I feel dumb for not knowing this.