How to use mouse.Target?

  1. What do you want to achieve?
    I am trying to make that when you’re mouse hover a block a selection block apears

  2. What is the issue?
    When i use mouse.Target and i print the type of block its says that its a part but when is use a if statement it does nothin

  3. What solutions have you tried so far?
    I have tried to add some print() but nothin got printed

local sample = script:WaitForChild("Sample")
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
for _,blocks in pairs(workspace.Blocks:GetChildren()) do
	local t = mouse.Target
	if t == blocks then
		sample.Parent = blocks
		sample.Adornee = blocks
	else
			local f = blocks:FindFirstChild("Sample")
			if f then
				f:Destroy()
			end
	end
end

The script is a local script in starter pack
theres a folder called “Blocks” in workspace
in blocks there is a bunch of parts name “Block”

3 Likes

You’re not triggering your loop at all. It runs when the script first loads up, but after that, the script is ended. You need to wrap your loop in a function and connect it to some sort of event, e.g.

local sample = script:WaitForChild("Sample")
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

sample.Parent = plr:WaitForChild("PlayerGui")

mouse.Move:Connect(function()
	local t = mouse.Target

	for _,blocks in pairs(workspace.Blocks:GetChildren()) do
		if t == blocks then
			sample.Adornee = blocks
		else
			if (sample.Adornee == blocks) then
				sample.Adornee = nil
			end
		end
	end
end)

You’re also Destroy()ing your SelectionBox, which will prevent you from reparenting it again. All you need to do is remove the Adornee when you don’t want to display it anymore. I’d recommend moving your SelectionBox to the PlayerGui. I’ve made some adjustments to your script above.

11 Likes

Thank you :smiley: (30 charss)

3 Likes