How to get a player from their mouse?

I’m trying to move a phone Gui, from a cloned tool, into the owner of that cloned tool’s PlayerGui. I know it would be simpler to just have the Gui in StarterGui, however that seems like bad practice to store a large amount of Gui’s in one place, especially when they are only neccessary in very select scenarios (The phone will only have a singular, non-important use). I have settled on using Tool.Equipped:Connect() to signal when the Gui should be reparented, and through reading, I’ve found that the Tool.Equipped call passes the Player’s Mouse as a parameter. I also know that you are able to grab a player’s specific mouse through the use of Player:GetMouse(). Therefore, it seems reasonable to be able to work backwards and get the player of a specific mouse. How would I go about this, if it’s even achievable?

Current Script:

local PhoneGUI = script.Parent
local Phone = PhoneGUI.Parent


Phone.Equipped:Connect(function(mouse)
	print(mouse.Parent) --Returns null
	
	local function phoneToggle()
		for i, v in pairs(PhoneGUI:GetChildren()) do
			if v.ClassName == "Frame" then 
				if v.Visible == true then 
					v.Visible = false
				else
					v.Visible = true
				end
			end
		end
	end
	phoneToggle()
	
	Phone.Activated:Connect(phoneToggle())
end)

Any help or advice would be appreciated!

I’m a little confused. When you get a player’s mouse, you already know who is going to be the player. Can you elaborate on where you want to get the player from?

A mouse can only be gotten and used via the client right? And if thats the case well,

local player = game.Players.LocalPlayer
1 Like

Ah sorry, the script is located inside the tool, which is stored in Replicated Storage. As is the Gui that I am trying to reparent. So when the tool is cloned, and brought to the player’s backpack (through a different script) they are then able to equipped it, firing this script and Tool.Equipped which passes the parameter of the Player’s mouse. The tool equip script is stored inside the part you interact with to pick up the tool.

When the tool is equipped, their character is gonna be tool.Parent so you can get the player with game.Players:GetPlayerFromCharacter(tool.Parent)

This is all located in a server script, I thought reparenting is frowned upon, if not impossible to do, through client scripts?

1 Like

So would I run game.Players:GetPlayerFromCharacter(tool.Parent) in the Connected function of Tool.Equipped?

Yes local player = game.Players:GetPlayerFromCharacter(tool.Parent)
(only when equipped)

2 Likes

Yes that would work but uh, you cant reparent the mouse I dont think

I’m reparenting the Gui, not the mouse :slightly_smiling_face:

2 Likes

I don’t think you can pass the player’s mouse if it’s located in the server script, but if you are trying to get the player from Tool.Equipped, @VonsRevenges has mentioned a method you can do to get the player.

2 Likes

Aye, his method worked seamlessly. Thank you all for your swift help :smile:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.