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

Get player who clicked SurfaceGui?

Hey, I'm trying to get the name of the player who has clicked on a Button on a Surface Gui.

Basically when the player clicks on the 'Collect' button I just need their name so I can edit their stats etc... But that part is irrelevant, the main problem is, I can't just use a LocalScript because when the player click 'Collect' it needs to update the Cash value that is displayed in the SurfaceGui so all other players can see that the Cash has been taken.

Any idea of a workaround?

2 answers

To change the stats from the client to the server you need a remote event, the event also has a parameter for the player going to and from the server.

Local Script

local remote = game:GetService("ReplicatedStorage"):WaitForChild("CollectMoney")

workspace.Part.SurfaceGui.TextButton.Activated:Connect(function()
	remote:FireServer()
end)

Server Script

local remote = Instance.new("RemoteEvent")
remote.Name = "CollectMoney"
remote.Parent = game:GetService("ReplicatedStorage")

remote.OnServerEvent:Connect(function(player)
	local stat = player:WaitForChild("leaderstats"):WaitForChild("CashValueNameHere")
	stat.Value = stat.Value + 100 -- Number Value Here
end)

The idea is you create a RemoteEvent which can then allow the Server and Client to communicate. When the button is clicked, the remote will tell the server to change the money / point system value of the player.

For the above example, the surface gui would be under a part named "Part" in the workspace with a TextButton under the gui. Everytime the button is clicked, the player would gain 100 of the applicable stat, where the stat would be found under a folder / model in the player named "leaderstats"

3 comments

0

Just a few questions. Where in the localscript is it sending some sort of player ID to the server?, Do I have to create the remote event beforehand due to the client sending it to nothing otherwise? And if so, why is it being created again in the server script? Lastly, how can I get the localscript to change the amount of cash that has been collected back to 0? So all other players can see it has

0

Remote Events from the client to the server tell the server who fired it, it sends the actual player instance, not their id. You can create the remote beforehand, but the server script here will do that for you. It needs to be created on the server so that there is a copy of the remote event on the server and not just the client (the same replicated storage is used by both entities).

0

Any changes to the player's cash value on the server will be replicated for all clients to see, any changes on the client will only be visible to the client. In order to revise the cash collect to zero, i'd have to look at the script you are using for that. Also based on what you said in another comment, you can use the :GetDescendants() method of the workspace and check that the item is a textbut

In order to get the player who clicked a SurfaceGui, you need to place the SurfaceGui in their PlayerGui. You can do it in two ways:

  1. Use a LocalScript to create the SurfaceGui, handle the click event locally, and use RemoteEvents to notify the server when the button is clicked

  2. Use a server script to create the SurfaceGui in the player's PlayerGui, set its Adornee to the part it should be displayed on. That way you know which player clicked the gui.

Example (server):

game.Players.PlayerAdded:Connect(function(plr)
	local gui = game.ServerStorage.Gui:Clone()
	gui.Parent = plr:WaitForChild("PlayerGui")
	gui.Button.MouseButton1Click:Connect(function()
		--now you can use the plr variable
	end)
end)

3 comments

0

I've tried both of these ways, but players are able to place down as many of these objects that generate money as they like. So when the the cash is collected from one, how is it supposed to identify which surface gui was pressed?

0

By creating a separate SurfaceGui for every player, you know which one was pressed

0

I'm semi there, i'm using the adornee method. The values for the cash etc, are generated in the model in workspace. But when I use the Gui to search for the values to update labels it still searches in PlayerGui instead of the model in workspace.

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