Making Gui not show up when a value is true

Hey there! I am making a setting in my game, where you are not able to see any gui that has to do with the racing part of my game, if you have a value that is true. I also would like to know how efficient is the method I am using
for sending messages for the gui. The serverscript for the race sends lots of for loops to gui scripts for it to run for everyone, (as far as I know), like this:

for i, player in ipairs(game.Players:GetPlayers()) do
	if player:WaitForChild("PlayerNoRaceMode").Value == false then
		player.PlayerGui.SpeedGUI.MapFrame.LocalScript.RemoteEvent:FireAllClients()
	end	
end

The problem is, the gui shows for everyone, regardless of whether the value is true or not, which doesn’t make sense. I notice though that it says FireALLClients(), so maybe it is just firing one and doing it for all, but, there is only one player in the game, me, and that person has the value to true.
And this also brings up my other question, is there a better way to do this, like instead of a for loop?

1 Like

You are sending multiple all client events to each clients remote event…

A few things I’d change. Perhaps run this only inside of a localscript. Remove the fireallclients and just check the value from the client via a replicatedstorage value.

Also it does show up for everyone because you are sending everyone a call to activate it.

1 Like

well it needs to be from the server since there are a lot of other server factors involved

wait(3)
local changeTimerEvent = game.ReplicatedStorage:WaitForChild("RaceEvent")
local otherChangeTimerEvent = game.ReplicatedStorage:WaitForChild("OtherRaceEvent")
local number = 30
local originalNumber = 30
local TimeLimit = 90
local waitTimer = false
local otherNumber = 15
local otherOriginalNumber = 15
local block = game.Workspace.RaceWay.BlockLine
local otherblock = game.Workspace.ElectricStretch.BlockLine
local otherOtherblock = game.Workspace.SwitchSwapRaceWay.BlockLine
local otherOtherOtherblock = game.Workspace.WaterWay.BlockLine
local words = ""
local playerCount = 0
local otherWaitTimer = false
function changePlayerCount(player)
	playerCount = playerCount - 1
	wait(3)
	if player.PlayerIsRacing.Value == true then
		player.PlayerIsRacing.Value = false
	end	
end
game.ReplicatedStorage.GameEndEvent.Event:Connect(changePlayerCount)
while true do
	words = "Intermission: "
	if number ~= 0 then
		number = number - 1
		changeTimerEvent:FireAllClients(number, words)
		wait(1)
	else
		for i, player in ipairs(game.Players:GetPlayers()) do
			if player:WaitForChild("PlayerNoRaceMode").Value == false then
				player.PlayerGui.SpeedGUI.MapFrame.LocalScript.RemoteEvent:FireAllClients()
			end	
		end
		game.ReplicatedStorage.HomeTownDerby.Value = 0
		game.ReplicatedStorage.ElectricStretch.Value = 0
		game.ReplicatedStorage.SwitchSwapRaceWay.Value = 0
		game.ReplicatedStorage.WaterWay.Value = 0
		words = "Vote For Course"
		number = ""
		changeTimerEvent:FireAllClients(number, words)
		game.ReplicatedStorage.StartVote:Fire()
		game.Workspace.ui_loading:Play()
		for i, player in ipairs(game.Players:GetPlayers()) do
			if player:WaitForChild("PlayerNoRaceMode").Value == false then
				player.PlayerGui.SpeedGUI.MapFrame.Event:Fire()
			end	
		end
		wait(20)
		words = game.ReplicatedStorage.WhichRaceWay.Value
		changeTimerEvent:FireAllClients(number, words)
		for i, player in ipairs(game.Players:GetPlayers()) do
			player.PlayerGui.SpeedGUI.MapFrame.TextLabel.Event:Fire(words)
		end
		wait(8)
		for i, player in ipairs(game.Players:GetPlayers()) do
			player.PlayerGui.SpeedGUI.MapFrame.OtherEvent:Fire()
		end
		wait(3)
		for i, player in ipairs(game.Players:GetPlayers()) do
			player.PlayerGui.SpeedGUI.MoveRaceScript.Event:Fire()
		end
		wait(2)
		waitTimer = true
		words = "Race Ends in: "
		number = TimeLimit
		otherWaitTimer = true
		while waitTimer == true do
			if otherNumber ~= 0 then
				otherNumber = otherNumber - 1
				otherChangeTimerEvent:FireAllClients(otherNumber)
				if otherNumber == 3 then
					local whichSong = math.random(1,4)
					if whichSong == 1 then
						game.Workspace.StartSounds.SFX:Play()
					end
					if whichSong == 2 then
						game.Workspace.StartSounds.GranTurismoCountdown:Play()
					end
					if whichSong == 3 then
						game.Workspace.StartSounds.SuperSmashBrosCountdown:Play()
					end
					if whichSong == 4 then
						game.Workspace.StartSounds.MarioKartWiiCountdown:Play()
					end
				end
				if otherNumber == 0 then
					otherNumber = nil
					otherChangeTimerEvent:FireAllClients(otherNumber)
					if block.CanCollide == true then
						block.CanCollide = false
					end
					if otherblock.CanCollide == true then
						otherblock.CanCollide = false
					end
					if otherOtherblock.CanCollide == true then
						otherOtherblock.CanCollide = false
					end
					if otherOtherOtherblock.CanCollide == true then
						otherOtherOtherblock.CanCollide = false
					end
				end	
				wait(1)
				if otherNumber == nil then
					otherNumber = 0
				end	
			elseif otherNumber == 0 then
				changeTimerEvent:FireAllClients(number, words)
				wait(1)
				for i, player in ipairs(game.Players:GetPlayers()) do
					if player.PlayerIsRacing.Value == true then
						playerCount = playerCount + 1
					end
				end
				while otherWaitTimer == true do
					if number ~= 0 then
						number = number - 1
						changeTimerEvent:FireAllClients(number, words)
						wait(1)
						if playerCount == 0 then
							number = 0
						end
					end
					wait()
					if number == 0 then
						for i, player in ipairs(game.Players:GetPlayers()) do
							if player.PlayerIsRacing.Value == true then
								player.Character:MoveTo(game.Workspace:FindFirstChildOfClass("SpawnLocation").Position)
								game.ReplicatedStorage.GameEndEvent:Fire(player)
							end
						end
						number = ""
						words = "Race Over!!!"
						game.Workspace.RaceWay.FinishLine.TouchScript.OtherEvent:Fire()
						game.Workspace.ElectricStretch.FinishLine.TouchScript.OtherEvent:Fire()
						game.Workspace.SwitchSwapRaceWay.FinishLine.TouchScript.OtherEvent:Fire()
						game.Workspace.WaterWay.FinishLine.TouchScript.OtherEvent:Fire()
						changeTimerEvent:FireAllClients(number, words)
						for i, player in ipairs(game.Players:GetPlayers()) do
							player.PlayerGui.SpeedGUI.EndOfRoundFrame.EndRoundScript.Event:Fire()
						end
						for i, player in ipairs(game.Players:GetPlayers()) do
							player.PlayerGui.SpeedGUI.MoveRaceScript.RestartEvent:Fire()
						end
						number = 0
						wait(15)
						otherWaitTimer = false
						waitTimer = false
						number = originalNumber
						otherNumber = otherOriginalNumber
						otherChangeTimerEvent:FireAllClients(otherNumber)
						block.CanCollide = true
						otherblock.CanCollide = true
						otherOtherblock.CanCollide = true
						otherOtherOtherblock.CanCollide = true
						words = "Intermission: "
						changeTimerEvent:FireAllClients(number, words)
						game.Workspace.RaceWay.FinishLine.TouchScript.Event:Fire()
						game.Workspace.ElectricStretch.FinishLine.TouchScript.Event:Fire()
						game.Workspace.SwitchSwapRaceWay.FinishLine.TouchScript.Event:Fire()
						game.Workspace.WaterWay.FinishLine.TouchScript.Event:Fire()
						for i, block in ipairs(game.Workspace.OutsideOuchies:GetChildren()) do
							block:Destroy()
						end
						game.Workspace.RaceWay.EndBigPart.Position = Vector3.new(138, 8, 634.75)
						game.Workspace.ElectricStretch.EndBigPart.Position = Vector3.new(730, 120.75, 634.75)
						game.Workspace.SwitchSwapRaceWay.EndBigPart.Position = Vector3.new(1341, 120.75, 634.75)
						game.Workspace.WaterWay.EndBigPart.Position = Vector3.new(-136.5, -160.25, -3619.75)
						wait(1)
					end
				end	
			end	
		end
	end
end
1 Like

Just a tip, doesn’t solve your problem, but useful anyways.

If that script is a local script, the one with the wait() at the top, you can replace the wait() with repeat wait() until game:IsLoaded.

It may do things you don’t want if you wait for a set amount of time.

1 Like

Also to solve your problem, if you only want it sending an event to one client you could just fire the one client. FireAllClients fires it for every player, so every player will get the event.

1 Like

Instead of using a repeat - until loop, you can check game:IsLoaded(), and if it is false, wait for the loaded event to fire. game.Loaded:Wait().

2 Likes

Interesting! Do you have to use an if statement or can you just put game.Loaded:Wait() at the top?

1 Like

You have to use an if statement as it only fires once.

1 Like

Ok, I think I fixed it, with this:

for i, player in ipairs(game.Players:GetPlayers()) do
	if player:WaitForChild("PlayerNoRaceMode").Value == false then
		game.ReplicatedStorage.MoveVote:FireClient(player)
	end	
end

though I still wonder if there is a better or easier way to do this…

1 Like