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?
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)
I wouldn't check to see if Mouse.Target is nil after I use it as an argument in a function.
: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)