Exclude local player from a table

Hello everyone.

I’m working on a voting GUI, but I’m making it so you can’t vote yourself. Here is what I have so far:

for i,v in pairs(votePlayers) do
		if v ~= game.Players.LocalPlayer then
			local vButton = Instance.new("TextButton")
			vButton.Text = v
			vButton.Parent = votingGui.Frame
		
			votingScript:Clone().Parent = vButton
		else end
	end

However, even when I run it, it will still display the local player. Is there anything that I’m doing wrong? Thank you in advance.

2 Likes

Make it so if they press the button to vote there self, it doesn’t run or makes the gui invisible

That’d add more lines of useless code. I’d rather take the easy option and just exclude them entirely.

1 Like

Change

v ~= game.Players.LocalPlayer

to

v ~= game.Players.LocalPlayer.Name

Because basing on your code I assume that votePlayers contains player names not player instances.

is your in pair loop a table of the players names?

I realized my error; I’m not able to use game.Players.LocalPlayer in a ServerScript. However, I don’t want to turn it into a LocalScript because I don’t like using Local Scripts to control GUI.

Is there any way to get around this?

Localscripts are primarily used to control UI or GUI elements in Roblox.
What are you talking about when you say you do not like using LocalScripts to control GUI?
Is there any particular reason why?

Yes, if that GUI is placed in PlayerGui. You can reference the player from here.
For example if the script was placed here:
image
You can reference the player by

local plr = script.Parent.Parent.Parent

I usually use ServerScripts to control GUI and when they appear on a player’s screen because I may do other serverwide functions with the same script. It’s a lot easier for me as I don’t have to do so much communication with the server. It’s a bit of a bad habit but it hasn’t caused me any major issue.

GUIs should only be controlled by local scripts. It’s not if you like or dislike it, it’s basically good practice, using server scripts can also affect performance.

It’s easier for you, but it’s harder for the players that play your game.
Why? Ping, anyone who has bad ping will not recieve something in time.

1 Like

The LocalPlayer property is nil on the Server;

That is what you should use, don’t put unnecessary load on the Server, by letting it handle UI for every single Client, use a local script for that purpose.

Also, with ServerScripts and FE , the Server is obscure of the server-script’s existence when it is in PlayerGui thus that server script doesn’t work, unless this got changed

2 Likes

Use a localscript for UI unless you want a highly latent game, where UI respond in about 2 seconds to a command localscripts can execute in mere miliseconds.
This is a bad habit and yes, you don’t just write bad code because it benefits you as @VegetationBush mentioned.

This is why remotes exist

Just call a remote event, they can still access that script since its in startergui and its not on a server. Hackers can easily exploit your game with that.

I understand that my current habits are bad; however, my original question has not been fully answered and instead I have been criticized.

We are answering your question: Use a localscript, this gets the localPlayer nice and easy.
Also, being criticised is how people learn; be it a builder, animator, scripter.

1 Like

Is this a table with all the players names in the server, when someone leaves remove that players name from the table. If you cant handle criticism then you cant be a scripter or a builder.

1 Like

Technically, we did answer your question. We are telling you to utilize local scripts, which will solve your problem. Using a local script, this would be your answer:

Make sure to mark TokoNanami’s answer as a solution as he is the one that actually answered the question

On the Forum, it is a good habit for developers to give the best solution they know; what you’re doing is discouraged practice, @fredrick254 only suggested a good idea but it’s up to you to decide whether you want to follow it though.


You won’t face any problems owing to the fact that you’re not in a real environment where there’s going to be several players who’ll potentially interact with UI frequently.

In your code you wrote :

for i,v in pairs(votePlayers) do
-- change this to 
for _, player in ipairs(votePlayers) do 

because as I’ve mentioned in other threads too, ipairs is built to work with contiguous arrays with numerical indices, and will provide you a quicker result.

Also, GetService() methods are better than dot syntax for services, there’s guaranteed instantiation.

I can suggest you another way to do this; through a Local Script

A local script :


 local Players = game:GetService("Players")
 local votePlayers = ...
 local currentPlayer = Players.LocalPlayer

 if votePlayers[currentPlayer] then
        i = table.find(votePlayers, currentPlayer)
        table.remove(votePlayers, i)
 else 
        print("Already absent from votePlayers")
end


  for _, player in ipairs(votePlayers) do
    	 local vButton = Instance.new("TextButton")
         vButton.Text = player.Name
   		 vButton.Parent = votingGui.Frame
  end

However, if votePlayers contains names only, then go ahead with @TokoNanami’s solution.

2 Likes

I wanted to apologize for acting so inappropriately yesterday.

I need to be better with handling criticism and I’ll never grow as a developer (or a person) without understanding that I’ll have to change some things to become smarter and better.

Thank you to everyone who was giving me constructive feedback and for being honest. It means a lot. I’ll try to act smarter and less irrationally going forward and I’ll take this to heart.

It was wrong of me to discard this very useful feedback. My sincerest apologies.