Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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

0
i think you can set the value as the object instead of the name CodinGlitch 36 — 6y
0
I tried that, didn't work :c PixelZombieX 8 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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)
Ad

Answer this question