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

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

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?

1 comment

0

TargetFilter perhaps?

1 answer

✓ Accepted answer

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)

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