If statement runs even though condition is NOT met

Unlike most people, my problem is unique- My if statement is running even though I don’t want it to.

local players = team:GetPlayers()
	for i, v in players do
		local gui = v:FindFirstChild("PlayerGui")
		if gui then
			if gui.Spectate.Enabled == true then					--if spectating then
				print("count increased")
				count += 1 
				v.Character:MoveTo(workspace.SpectatorBrick.Position)    --teleports player to a spectator hub so that they don't interfere with gameplay
			end
		end
	end
	if #players == count then                 --if all players are in spectator mode (the "Spectate" LocalScript in their PlayerGui is set to TRUE) then that means the whole lobby is DEAD.
		print("all players dead")
		workspace.VOTE_TOGGLE.Enabled = true       --This enables a voting procedure, not important.
	end

As you can see, I am the only one in the server, yet the count increases.

You can see that “Spectate” in my PlayerGui has been disabled but the if statement that runs only when Spectate is enabled is running regardless of whether or not the script is actually enabled

I have literally no idea what’s causing this or how to fix this. Is it an engine bug? Is my logic wrong? I don’t know! This is going against my scripting fundamentals…

P.S: The script works perfectly okay the first two times that I vote but on the third try this happens (the problem in question occurs)

2 Likes

Your script which checks whether the gui is enabled or not is a server script. If you changed the Enabled property of the gui in a client-sided script, the server won’t pick up on that change.

2 Likes

They should probably fire a remote event or something like that to increase the count and communicate if the player is spectating or not.

Dude the GUI is not being enabled, the name of the script is Spectate. It checks whether or not the script named “Spectate” is Enabled

Why would I do that when I already have access to each and every player’s PlayerGui and can just directly see whether or not they are currently in Spectator (Spectate.Enabled = true) mode?

My point was, if you changed the Enabled property of the script on the client, it won’t automatically change on the server. Maybe the server thinks that it’s enabled.

So you’re saying I should disable the Gui and then check if the Spectate script is enabled?

Also the script isn’t under any ScreenGui or anything- it’s a direct child of the StarterGui/PlayerGui

Do you change anything on the client?

I think you all should read the P.S. I added to the original Post. If you want a screen recording I can get that

The spectate script succesfully disables itself after the voting procedure ends. Also, the Spectating UI is removed by said spectate script after voting procedure ends.
Besides that, no, not really.

Okay, that’s the issue - Spectate disables itself, but since it’s just a LocalScript, there’s no change on the server, and the server script thinks Spectate is still enabled. You should send a remote event to the server when Spectate is disabled so the server knows.

Alright, I’ll try that. I’ll get back to you when I’m done

How would I use a remote event to communicate the fact to the server that the spectate script is disabled? Do I have to disable the spectate script from the server’s end?

I don’t see why the server needs to be involved

ys Is telling me that the server is unaware that the spectate script is disabled. The script mentioned in the post is a server script in the workspace

@ys3741 is correct, but I don’t believe you need to be doing anything on the server in the first place. Clients own their characters, so they can teleport themselves. At most, you’ll have a RemoteFunction to retrieve active matches, should that data not already be replicated to the client

So you’re telling me I can use a LocalScript to change the Character’s position and that’ll reflect on the server?

How about this line?

workspace.VOTE_TOGGLE.Enabled = true       --This enables a voting procedure, not important.

VOTE_TOGGLE is in workspace

Yes. That is what I am telling you