#Highlight Tool not working (mouse bug)

32 messages · Page 1 of 1 (latest)

glossy tartan
#

so im trying to make a tool, which when equipped lets you hover over players and when hovering it adds a highlight to them. it dosent even get to printing the target so far in this script and i cannot figure out why. pls help.

#
local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent

local currentHighlight = nil
local currentTarget = nil
local moveConnection = nil

local function createHighlight(character)
    if currentHighlight then
        currentHighlight:Destroy()
    end

    local highlight = Instance.new("Highlight")
    highlight.Parent = character
    highlight.OutlineColor = Color3.fromRGB(255, 0, 0)
    highlight.FillColor = Color3.fromRGB(255, 0, 0)

    currentHighlight = highlight
    currentTarget = character
end

local function onMouseMove()
    local target = mouse.Target
    if target then
        warn("target: ", target, target.Name, target.Parent)
        local targetPlayer = players:GetPlayerFromCharacter(target.Parent)
        if targetPlayer and targetPlayer ~= player then
            if currentTarget ~= target.Parent then
                print("hovering: ", targetPlayer.Name)
                createHighlight(target.Parent)
            end
        else
            if currentHighlight then
                currentHighlight:Destroy()
                currentHighlight = nil
                currentTarget = nil
            end
        end
    else
        if currentHighlight then
            currentHighlight:Destroy()
            currentHighlight = nil
            currentTarget = nil
        end
    end
end

tool.Equipped:Connect(function()
    warn("tool equipped")

    if moveConnection then
        moveConnection:Disconnect()
    end
    moveConnection = mouse.Move:Connect(onMouseMove)
end)

tool.Unequipped:Connect(function()
    if currentHighlight then
        currentHighlight:Destroy()
        currentHighlight = nil
        currentTarget = nil
    end
    
    if moveConnection then
        moveConnection:Disconnect()
        moveConnection = nil
    end
end)
#

localscript under tool

rare trail
#

tbh, didnt even know mouse.move was a thing - have you tested it works separate to this main script? the mouse.move event that is

#

i think it's quite old, so there could be issues with it, but the wiki does suggest it may still work

glossy tartan
#

do you know any alternatives to mouse.move

rare trail
#

userinputservice

#

you can do UserInputService.InputChanged and then detect mouse movement

glossy tartan
#

uh what

#

i didnt know that

#

the flip

rare trail
#

indeedly

#

it is a little finicky to learn it at the start but you get used to it

#

cause it has a few odd bits and bobs

glossy tartan
#

so wait would it be something like this

#

game:GetService("UserInputService").InputChanged:Connect(function(input, proc)
if not proc and input ==
end)

#

whats the thing to get the mouse

rare trail
#

input.Position gives you a Vector3 of the mouse position

glossy tartan
#

damn

rare trail
#

but, i suppose you could just use that for the event, and then use the Mouse object for Mouse.Target or whatever you wanna do

#

maybe its cheesing it a little

#

alternative is casting a ray from the camera at the mouse position which i always find annoying to set up

glossy tartan
#

hmmmmmmmmmm

#

ok i think i figured it out

#

userInputService.InputChanged:Connect(function(input, proc)
input:GetPropertyChangedSignal("Position"):Connect(function()
warn(mouse.Target)
end)
end)

#

this works im pretty sure

#

ty for letting me know about the userinputservice way

rare trail
#

i dont think it will, input is an object created each time the inputchanged event fires

#

lmk tho

ember terrace
wanton tulipBOT
#

studio** You are now Level 4! **studio

ember terrace
#

mouse.Hit and userinputservice