How would I go about getting something from a table, and then making it the text of something?

Basically, I just want to get a player name from this table, then put that player’s name as text for a gui button. I’ve messed around with most of the table related things I know, but I don’t know where to go from here.
I also need assistence with figuring out how to do this multiple times, with different player names, all retrieved from the same table, but that’s a post for another day.
Any help is greatly appreciated!
Anyways, here’s the script:

local electionevent = game:GetService("ReplicatedStorage").Election

local function onServerEventFired(player)
	local electiongui = game.StarterGui:FindFirstChild("TRUEELECTION")
	local Electiontable = {
		
	}
	Electiontable[player.Name] = true
	if Electiontable[player.Name] then print(Electiontable)
			print("test")
		player.PlayerGui.TRUEELECTION.Frame:FindFirstChild("1").Text = Electiontable[1]
			task.wait(10)
		player.PlayerGui.TRUEELECTION.Enabled = true

			
		end
end
electionevent.OnServerEvent:Connect(onServerEventFired)
2 Likes

If I understood you right -

You could simply make a system where you, lets say - vote for people;
and you want their names to be shown on a gui.

You can fire an event [I’d prob use a function, but you can use event aswell].
So whenever the player votes for a player, it’d insert than name into a table [ make sure that player wasnt added to your table before, so won’t count him twice or more].

And, now, you can make a gui for everyone in the game, and for each player that’s in your table, you can display his name under , lets say ‘Voted Players’ for the e.g.
And it’d be shown to everyone. You might need to use a loop there.

2 Likes

I think you might be a bit confused. The script’s remoteevent is fired whenever a “become candidate” button is pressed by the player. The script is meant to get the players who’ve decided to become candidates, then put their names as a GUI button’s text. My issue is I’m unsure of how to go about getting a player name from my table and setting it as a gui button’s text, as I don’t know how to do this.

1 Like

Also,‘Electiontable[1]’ will give you nil here. The reason, is because you need to print the value of that player in this case, you did 'Electiontable[player.Name] = true, then you should do instead :

player.PlayerGui.TRUEELECTION.Frame:FindFirstChild(“1”).Text = Electiontable[player.Name] – but, that’ll make the text as ‘true’ , because you set the player.name as true [ in your table]

image

1 Like

I see what you’re getting at, I’m just somewhat confused as I feel like I’m being hit with lots of information lol.

2 Likes

There’re 2 ways [as I know], you could get the player from your table:

image

Now that you’ve told me that, these steps might help you:

  1. Invoke an event to the server whenever that button is pressed by the player.
  2. On the server, insert that player’s name into the table [make sure he’s not there already, you can use if not table.find(‘tableName’,‘playerName’) then…
    3)Return true after he’s been added to there, [make sure the invoke on the client is a variable].
    4)Check if your invoke variale is true. If it is, change the text’s name to the player’s name. [you could also add ‘VOTED’ or something like this
1 Like

Use a remote function instead, do you could return stuff to the client and change his button text .

Well, I’ve been attempting to do what you’ve said, but I’m having a bit of an issue. I get an error saying “attempt to index nil with ‘Name’” I understand that the issue is it cannot find the player whom clicked the button, but I’m just not sure how to fix it. I’d also like some feedback on my current attempt of the idea. Very recently became a scripter, so stuff like this is quite challenging for me to overcome.
Localscript that triggers the remotefunction:

local stor = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local elc = stor.at
script.Parent.MouseButton1Click:Connect(function()
	print("Registered")
	elc:InvokeServer(player)
	player.PlayerGui.register.Enabled = false

	
		end)

serverscript:

local stor = game:GetService("ReplicatedStorage")
local elec = stor.Election
local elc = stor.at
	local Electiontable = {
		
}
local function onFired(player)
	if not table.find(Electiontable, player.Name) then
		table.insert(Electiontable, player.Name)
		print(Electiontable[1])
		elec:FireAllClients()
			
	end
	end
elc.OnServerInvoke = onFired()
1 Like
  1. When you’re invoking server from the client, the 1st parameter is automatically the player, so you dont need to provide him as the parameter there (remove it from the invoke params]

  2. You cant fire all clients with an invokeFunction. -Remove ‘elec:FireAllClients()’

  1. I tried removing it from the invoke in the localscript, but the error still occurs. Please do elaborate on how I could solve this

Did you remove ‘elec:FireAllClients()’?

1 Like

Yes. Would elc:InvokeClient() be what I should do instead of fireallclients?

No, no need to do that, you can just return true and
change on the client this line: elc:InvokeServer() to this:
local myEvent = elc:InvokeServer()
if myEvent == True then
//code goes here on the client
end

after ‘print(Electiontable[1])’ on the server, make sure to return true

Alright. I’m still not sure how to get around this error, though. Just not exactly sure what to remove, even after I tried your advice.
image
Anyways, here’s the scripts in their current forms:
localscript:

local stor = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local elc = stor.at
script.Parent.MouseButton1Click:Connect(function()
	print("Registered")
	elc:InvokeServer()
	player.PlayerGui.register.Enabled = false
	if elc == true then
		print("works")
	end
	
		end)

serverscript:

local stor = game:GetService("ReplicatedStorage")
local elec = stor.Election
local elc = stor.at
	local Electiontable = {
		
}
local function onFired(player)
	if not table.find(Electiontable, player.Name) then
		table.insert(Electiontable, player.Name)
		print(Electiontable[1])
		return true
			
	end
	end
elc.OnServerInvoke = onFired()

change this : elc:InvokeServer()

to this : local myEvent = elc:InvokeServer()

and then :

if myEvent == true then
print(“works”)
end

this look right, or am I being dumb?
image

1 Like

Test it it looks fine to me. Update

error still happens