How to pass info from a GUI button to the server?

Goal
There are 6 GUI buttons. I want to have one RemoteEvent fired to the server when a player clicks on one of the 6 buttons, in order to create a ServerEvent when one of the 6 buttons is clicked.

Issue
Passing on information using the script below has not worked for me. I know why.
Button.Name cannot be accessed by the server.

Localscript:

for _, Button in pairs(ButtonGroup) do
	
	Button.MouseButton1Click:Connect(function()
		local ButtonName = Button.Name
		print(ButtonName)
		ChosenRemote:FireServer(Player, ButtonName)
		print("Sent")
	end)
	
end

Topic Question / Alternative?
Is there another way for the server to know which button is clicked by the player?
In the past I used 6 different remotes, but I feel like using one remote is better.

Other tried solutions
I came across a few posts on the forum that had a similar question, however, these did not answer my question. I have tried several hours to make this work.

The server in your case should receive the buttonname as a String, you don’t send a reference to an object.

--Serverside
ChosenRemote.onServerEvent:Connect(function(player, ButtonName)
   print(ButtonName) 
end)

should definitely print your Button Name.

Did you make sure your ServerScript can actually execute? Does the Server actually receive the event?

1 Like
Screenshot of Output

Output

The button is indeed called “Daisy” in this particular case. So that’s correct. The server seems to print my playername again, which is strange.

EDIT : added the ServerScript

ServerScript
ChosenRemote.OnServerEvent:Connect(function(player, ButtonName)

print(player)

print(ButtonName)

end)
1 Like

Actually your problem might be that you reveive the player as paremeter 2 times.
Like:

ChosenRemote.onServerEvent:Connect(function(player, player, ButtonName)

Cause when you send an Event, your client Player is always the first paremeter, you don’t need to send it yourself.

Change it to that:

for _, Button in pairs(ButtonGroup) do
	
	Button.MouseButton1Click:Connect(function()
		local ButtonName = Button.Name
		print(ButtonName)
		ChosenRemote:FireServer(ButtonName)
		print("Sent")
	end)
	
end

(Just removed the player as Paremeter)

1 Like

That did the job! Thanks for your time!

1 Like

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