How to get player from button clicked

Im trying to make a party system and when I make the button, a clone, I need to detect which player clicks it so they join the party. However, im not sure how to do this. I tried to put player into the mousebuttonclicked function but that prints nil.

local frame = script.Parent

game.ReplicatedStorage.CreateParty.OnServerEvent:Connect(function(plr)
	if plr.RegularValues.PartyCreation.Value == false then
		
		game.ServerStorage.ServerValues.Servers.Value += 1
		local val = game.ServerStorage.ServerValues.Servers.Value 
		local players = {plr.Name}
		local partyBox = frame.PartyExample:Clone()
		
		partyBox.Parent = frame
		partyBox.Visible = true
		partyBox.Position = UDim2.fromScale(0.046, 0.013 + (val - 1)*0.060)
		partyBox.Name = "PartyBox"
		partyBox.Text = plr.Name.. "'s Party (1/5)"
		
		partyBox.MouseEnter:Connect(function()
			partyBox.BackgroundTransparency = 0.4
		end)
		partyBox.MouseLeave:Connect(function()
			partyBox.BackgroundTransparency = 0.6
		end)
		print(players)
		partyBox.MouseButton1Click:Connect(function(player)
			print(player)
			if player.RegularValues.PartyCreation.Value == false then
				table.insert(players, player)
			end
		end)
	end

end)

I think you need to familiarise yourself between the role of Local scripts and Server scripts.

Local scripts = interacting with the player, you can access this via game.Players.LocalPlayer . Should go in Starter gui or anything named Starter. They CANNOT interact with anything like Server storage or ServerScriptService for security purposes.

Server Scripts = doing stuff for the server (everyone) should only be placed in ServerScriptService and ServerStorage

Communication is to be done via Remote events or Remote functions.

You also need to learn about memory leaks and where you placing connections. The MouseButton1Click does not automatically disconnect so every time your firing that remote event it makes another connection which is never removed. This can build up and cause issues so I suggest moving it outside the event

1 Like

local player=game.Players.LocalPlayer assuming it’s a local script since it’s UI we are talking about
also note that when cloning a button and placing it into the playergui, the local script inside the button becomes active which means the code inside the script also becomes functional.

1 Like