How do you get the player from MouseButton1Click

The title says it all! I looked up “MouseButton1Click” to see if I could find a solution, but I didn’t find anything

6 Likes

Connect it to a function with the first parameter as player.
For example:

button.MouseButton1Click:Connect(function(player)
print(player) -- it will print the players name.
end)
5 Likes

Well I tried this and it didn’t work, and I already set up the teams

''''
local D = game.Teams:WaitForChild("D")
script.Parent.MouseButton1Click:Connect(function(player)
   player.Team = D.Team
end)

    '''

Serverscript

1 Like

You have to manually define the player. GUI scripts are most often done on a local script that can access the local player with ease which is why you must define it.

local player = game.Players.LocalPlayer

2 Likes

I think you mistake is that you said D.Team, but i might be wrong, any errors?

1 Like
  ''''
local Class = game.Teams:WaitForChild("Class")
   script.Parent.MouseButton1Click:Connect(function(player)
   player.Team = Class
end)
    
       '''

Serverscript

1 Like

This worked for me:


local Team = game.Teams:WaitForChild("Team")

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	game.Players[player.Name].Team = Team
end)

This is why it is imperative that you search for similar posts before posting, or look at the DeveloperHub, the event just doesn’t pass the player as an argument so this:

won’t work either.

MouseButton events are replicated across the client-server boundary, you can bind functions to them from a server-script if you manage to get the GuiObject - though it is advised to just handle this stuff locally, that way it’ll be even easier

-- LocalScript
local player = game.Players.LocalPlayer
local object = script.Parent -- Gui Button Object

local function onClicked()
   print(player.Name)
end

-- Object.Activated:Connect(onClicked) -- MouseButton events might be deprecated in the future, if you don't need to listen for such input through the server, use Activated over MouseButton1Click
Object.MouseButton1Click:Connect(onClicked)

I typed that, but after that i fixed my mistake and made it work and i’m also very new to scripting.

Well since it is in a local script, other players don’t know you are on that team, how can I fix that?

By using my script:


local Team = game.Teams:WaitForChild("Team")

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	game.Players[player.Name].Team = Team
end)

Server Script inside part

Players.Kamlkaze_Kid.PlayerGui.ScreenGui.Frame.Scientific Department.Script:4: attempt to index nil with ‘Name’ I edited your script

local Team = game.Teams:WaitForChild("Scientific Department")

script.Parent.MouseButton1Click:Connect(function(player)
	game.Players[player.Name].Team = Team
end)
1 Like

Oh, i didn’t know it was in a gui, let me fix that.

2 Likes

First add a remote event to replicated storage:
Local Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

script.Parent.TextButton.MouseButton1Click:Connect(function()
	RemoteEvent:FireServer()--Fires remote event
end)

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

local Team = game.Teams:WaitForChild("Team") -- Change this to the name of your team.


RemoteEvent.OnServerEvent:Connect(function(player)--If the remote event is received 
	game.Players[player.Name].Team = Team
end)

I hope this helps!

12 Likes

Where do I put the scripts???

2 Likes

Put it in ServerScriptService or workspace but i suggest ServerScriptService because it’s a nice place to put scripts and only scripts.

3 Likes

What if there is no team in the game?

1 Like