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

Why doesn’t “mouse.Target” work on a player?

Asked by 4 years ago

So right now, I’m trying to create a sort of “click a player for something to happen” kind of thing. However, i run into my first problem. When I type

script.Parent.Equipped:connect(function(Mouse)
local Player = game.Players:GetPlayerFromCharacter(Mouse.Target)
if Mouse.Target ~= nil and Player then
print(Mouse.Target.Name)
end
end)

For some reason, the player doesn’t get rendered(also not printed) and only the objects around me get printed! Can someone tell me what’s happening?

0
TargetFilter perhaps? greatneil80 2647 — 4y

1 answer

Log in to vote
1
Answered by
Rinpix 639 Moderation Voter
4 years ago

When you hover over another player's character, Mouse.Target is the part inside of their character that your mouse is hovering over, not the entire character model.

To get the character model, just do this:

local Player = game.Players:GetPlayerFromCharacter(Mouse.Target.Parent)

If your mouse is hovering over someone's character, it should work and say that you're hovering over a player's character.

Also, two things.

script.Parent.Equipped:connect(function(Mouse)
    local Player = game.Players:GetPlayerFromCharacter(Mouse.Target.Parent)
    if Mouse.Target ~= nil and Player then
        print(Mouse.Target.Name)
    end
end)
  1. I wouldn't check to see if Mouse.Target is nil after I use it as an argument in a function.

  2. :connect is deprecated, meaning it has a newer version and it may not work properly in the future; use :Connect instead. It's just unsafe to use deprecated functions.

Here's the full version of the fixed code:

script.Parent.Equipped:Connect(function(Mouse)
    if Mouse.Target ~= nil then
        local Player = game.Players:GetPlayerFromCharacter(Mouse.Target.Parent)
        if Player then
            print(Mouse.Target.Name)
        end
    end
end)
Ad

Answer this question