Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can you detect once your Mouse.Target is not the same as before??

I'm trying to make a SelectionBox and a GUI to appear once I put my Mouse on that part. I got that part over with, but I'm stumped when it comes to removing the SelectionBox and the GUI once the mouse is not detecting that part anymore. I've already tried a method but since there will be a lot of parts around the map it doesn't work correctly.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Previous = ""

Mouse.Move:connect(function()
	if Mouse.Target.Name == "Plant" then
		local hitbox = Instance.new("SelectionBox", Mouse.Target)
		hitbox.LineThickness = 0.05
		hitbox.Adornee = Mouse.Target
        Player.PlayerGui.ScreenGui.Frame.Visible = true
 		Player.PlayerGui.ScreenGui.Frame.TextLabel.Text = Mouse.Target.Hits.Value .. "/ 3"	
        Previous = Mouse.Target.Name
	   else 
		workspace[Previous].SelectionBox:remove()
		Player.PlayerGui.ScreenGui.Frame.Visible = false
	end
	
end)

It's located in a LocalScript in StarterGui

2 comments

0

i think you can set the value as the object instead of the name

0

I tried that, didn't work :c

1 answer

✓ Accepted answer

there is more than one part, so if it looks for it, it might look for the wrong one. by setting it as an object, you get directly to the object instead of looking for in in the workspace.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Previous = nil
local selected = false

Mouse.Move:connect(function()
    if Mouse.Target.Name == "Plant" then
	    if selected == false then
	        selected = true
            local hitbox = Instance.new("SelectionBox", Mouse.Target)
            hitbox.LineThickness = 0.05
            hitbox.Adornee = Mouse.Target
            Player.PlayerGui.ScreenGui.Frame.Visible = true
            Player.PlayerGui.ScreenGui.Frame.TextLabel.Text = Mouse.Target.Hits.Value .. "/ 3"  
            Previous = Mouse.Target
        end
       else 
        if Previous then
	        if Previous:FindFirstChild("SelectionBox") then
		        Previous.SelectionBox:remove()
                Player.PlayerGui.ScreenGui.Frame.Visible = false
	        end
        end
        selected = false
    end

end)

This discussion is preserved as it was. New answers, comments, and votes are closed.