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

How would i get player info by the mouse rolling over one?

In the past, i had tried this to fetch me variables with player's name and team:

repeat wait() until game.Players.LocalPlayer
repeat wait() until game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
player = game.Players.LocalPlayer
Mouse = player:GetMouse()
Identifier = game.Players.LocalPlayer.PlayerGui.CyborgGui.CyborgVision.Identifier
if player.Mouse.Target:GetPlayerFromCharacter()then
	Player = player.Mouse.Target
	PlayerName = Player.Name
	PlayerTeam = Player.TeamColor

--This is not a whole script, but a part of it

And it doesn't do anything, really. The variables won't fetch the things. What is wrong?

(I will post the rest of the script if it helps.)

1 answer

✓ Accepted answer

player.Mouse isn't a thing. If you're referring to the mouse object you made, just use your Mouse variable (canonical names are lowercase though)

Arbitrary objects don't have a GetPlayerFromCharacter method. That's a method of the Players service.

In addition, the Target is the Part, not, the model that the part is in. It can also be nil, so be careful.


Here is one way to put all of that together into a fix:

local target = mouse.Target
if target then
	-- Is hovering over something (not the sky)

	local character = target -- The *part* hovering over
	repeat
		character = character.Parent
	until character:IsA("Model") or character:IsA("Workspace")
	-- To work on Hats, Tool, Gear, we have to
	-- go up an unknown number of levels
	-- Until the workspace, or a model

	local targetPlayer = game.Players:GetPlayerFromCharacter(character)
	if targetPlayer then
		-- `character` is the Character model of player `targetPlayer`
		print(targetPlayer.Name)
	end
end

Additional note:

This will only happen once. -- When the script loads.

For the effect to be useful, this obviously needs to be happening repeatedly; simplest way is to use a while true do wait() loop.

2 comments

0

Doesn't work - won't recognize global "mouse" because it's nil. Then again, if i actually do add a mouse with GetMouse() it still doesn't work.

0

"canonical names are lowercase". What does "doesn't work" mean? 1) You can't hover over your own character 2) You have to hover over an actual player, not just a humanoid (so you would have to test with a server)

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