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
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)
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.
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.
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.
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?
@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