Script for gui works for run, but not play

Hi. I am scripting something that makes either a flood or a lawnmower attack for my game, and the script seems to be doing fine. It is supposed to give a countdown until the next event, say what it is, and so on. It works when I click “run” under test very well, but runs into problems like the countdown not showing when i do “play” under run. I am not sure how to fix this.

This is the script in workspace, where the text that is changing at the bottom belongs under startergui.

--Variables
local sprinklers = game.Workspace.Sprinklers
local check = true
local S1V = sprinklers.Sprinkler1.Water.ParticleEmitter.Transparency
local S2V = sprinklers.Sprinkler2.Water.ParticleEmitter.Transparency
local S3V = sprinklers.Sprinkler3.Water.ParticleEmitter.Transparency
local S4V = sprinklers.Sprinkler4.Water.ParticleEmitter.Transparency
local S5V = sprinklers.Sprinkler5.Water.ParticleEmitter.Transparency
local S6V = sprinklers.Sprinkler6.Water.ParticleEmitter.Transparency
local S7V = sprinklers.Sprinkler7.Water.ParticleEmitter.Transparency
local S8V = sprinklers.Sprinkler8.Water.ParticleEmitter.Transparency
local S9V = sprinklers.Sprinkler9.Water.ParticleEmitter.Transparency

--Loops

for countdown = 10 , 0, -1 do
	game.StarterGui.ScreenGui.TextLabel.Text = "Safe. Next event will occur in " .. countdown .. " seconds"
	wait(1)
	
	if countdown == 0 then
		local event = math.random(1,2)
		if event == 1 then
			game.StarterGui.ScreenGui.TextLabel.Text = "Flood! Get to the high point of the castle!"
			S1V = 0
			S2V = 0
			S3V = 0
			S4V = 0
			S5V = 0
			S6V = 0
			S7V = 0
			S8V = 0
			S9V = 0
		elseif event == 2 then
			game.StarterGui.ScreenGui.TextLabel.Text = "Lawnmower! Get to the cave!"
		end
	end
end

I am not sure how to fix this because I have never once faced this problem before, even when making other GUIs.

1 Like

judging from this script im guessing this is a server script and you are changing the StarterGui. which wouldn’t work for obvious reasons…
each player has the ui under Players.PlayerGui not under startergui.

regarding the changes i suggest you using remoteevents/functions that fire to client and let the client change the text of it but for simplicity this should work. IFF (if and only if) you have the screengui named “ScreenGui” for all players and textlabel is called “TextLabel”. also S1V to S9V wouldnt work since you are storing the transparency of the patricleemitter. ALSO particleemitters transparency is not a integer nor a floating point its a NumberSequence (i suggest you looking this up for better explanation).

local function ChangeText(NewText)
	for _, Player in pairs(game.Players:GetChildren()) do
		local PG = Player:WaitForChild('PlayerGui');
		PG.ScreenGui.TextLabel.Text = NewText; --Make sure these exists;
	end
end

for countdown = 10 , 0, -1 do
	ChangeText("Safe. Next event will occur in " .. countdown .. " seconds");
	--game.StarterGui.ScreenGui.TextLabel.Text = "Safe. Next event will occur in " .. countdown .. " seconds"
	wait(1)
	if countdown == 0 then
		local event = math.random(1,2)
		if event == 1 then
			ChangeText("Flood! Get to the high point of the castle!");
			--game.StarterGui.ScreenGui.TextLabel.Text = "Flood! Get to the high point of the castle!"
		elseif event == 2 then
			ChangeText("Lawnmower! Get to the cave!");
			--game.StarterGui.ScreenGui.TextLabel.Text = "Lawnmower! Get to the cave!"
		end
	end
end
1 Like

Since I am a little confused on the top part, does this make sure that everyone has the same timing or somewhere near it? Because I do not want events happening in multiple different times at once when they are no where near each other.

1 Like

Ok so I put the script under startergui and changed it like you said and it kind of works, except that the time AND event is both based on the individual person, meaning they would be at different times and could be different events, which I don’t want, because I want it to all happen at the same/very similar time and same event.

1 Like
-- Fire the client instead of making it visible on the server.
local sprinklers = game.Workspace.Sprinklers
local check = true
local S1V = sprinklers.Sprinkler1.Water.ParticleEmitter.Transparency
local S2V = sprinklers.Sprinkler2.Water.ParticleEmitter.Transparency
local S3V = sprinklers.Sprinkler3.Water.ParticleEmitter.Transparency
local S4V = sprinklers.Sprinkler4.Water.ParticleEmitter.Transparency
local S5V = sprinklers.Sprinkler5.Water.ParticleEmitter.Transparency
local S6V = sprinklers.Sprinkler6.Water.ParticleEmitter.Transparency
local S7V = sprinklers.Sprinkler7.Water.ParticleEmitter.Transparency
local S8V = sprinklers.Sprinkler8.Water.ParticleEmitter.Transparency
local S9V = sprinklers.Sprinkler9.Water.ParticleEmitter.Transparency

--Loops

for countdown = 10 , 0, -1 do
	game.StarterGui.ScreenGui.TextLabel.Text = "Safe. Next event will occur in " .. countdown .. " seconds"
	wait(1)
	
	if countdown == 0 then
		local event = math.random(1,2)
		if event == 1 then
			game.ReplicatedStorage.RemoteEvent1:FireClient()
		elseif event == 2 then
			game.ReplicatedStorage.RemoteEvent2:FireClient()
		end
	end
end

Local script (in starter gui)

local sprinklers = game.Workspace.Sprinklers
local S1V = sprinklers.Sprinkler1.Water.ParticleEmitter.Transparency
local S2V = sprinklers.Sprinkler2.Water.ParticleEmitter.Transparency
local S3V = sprinklers.Sprinkler3.Water.ParticleEmitter.Transparency
local S4V = sprinklers.Sprinkler4.Water.ParticleEmitter.Transparency
local S5V = sprinklers.Sprinkler5.Water.ParticleEmitter.Transparency
local S6V = sprinklers.Sprinkler6.Water.ParticleEmitter.Transparency
local S7V = sprinklers.Sprinkler7.Water.ParticleEmitter.Transparency
local S8V = sprinklers.Sprinkler8.Water.ParticleEmitter.Transparency
local S9V = sprinklers.Sprinkler9.Water.ParticleEmitter.Transparency
game.ReplicatedStorage.RemoteEvent1.OnClientEvent:Connect(function()
coroutine.wrap(
			for i, v in pairs(game.Players:GetChildren()) do
				local playergui = v:FindFirstChild("PlayerGui")
			if playergui then
				playergui.ScreenGui.TextLabel.Text = "Flood! Get to the high point of the castle!"
			end
			end
)()
			S1V = 0
			S2V = 0
			S3V = 0
			S4V = 0
			S5V = 0
			S6V = 0
			S7V = 0
			S8V = 0
			S9V = 0
end)
game.ReplicatedStorage.RemoteEvent2.OnClientEvent:Connect(function()
coroutine.wrap(
			for i, v in pairs(game.Players:GetChildren()) do
				local playergui = v:FindFirstChild("PlayerGui")
			if playergui then
				playergui.ScreenGui.TextLabel.Text = "Flood! Get to the high point of the castle!"
			end
			end
)()
			S1V = 0
			S2V = 0
			S3V = 0
			S4V = 0
			S5V = 0
			S6V = 0
			S7V = 0
			S8V = 0
			S9V = 0
end)
1 Like

So just to make sure this will make everyone see the same message at the top at the same time?

2 Likes

Yes, gui manipulation should be done with local scripts. So just create 2 remote events in ReplicatedStorage.

so there’s actually some errors with the code provided since :FireClient() needs to take in a player as a parameter. I suggest you using FireAllClients() since this doesnt need to take in player parameter, and also instead of having 2 different remoteevent for just changing text labels you should just use only 1 remoteevent that handles the changing of texts by sending in the new text as a parameter.
(Also fix the transparency part as stated above)
So the final product should look as follows:
(Make sure to add a remoteevent named RemoteEvent and place it inside ReplicatedStorage)

--This is the serverscript
local sprinklers = game.Workspace.Sprinklers
local remoteevent = game.ReplicatedStorage:WaitForChild('RemoteEvent');
local check = true
--Loops

for countdown = 10 , 0, -1 do
	RemoteEvent:FireAllClients("Safe. Next event will occur in " .. countdown .. " seconds");
	wait(1)
	
	if countdown == 0 then
		local event = math.random(1,2)
		if event == 1 then
			RemoteEvent:FireAllClients("Flood! Get to the high point of the castle!");
		elseif event == 2 then
			RemoteEvent:FireAllClients("Lawnmower! Get to the cave!");
		end
	end
end
--This is a localscript
local RemoteEvent = game.ReplicatedStorage:WaitForChild('RemoteEvent');
local PG = game.Players.LocalPlayer:WaitForChild('PlayerGui');
RemoteEvent.OnClientEvent:Connect(function(NewText)
    PG.ScreenGui.TextLabel.Text = NewText;
end)

as you can see this is way cleaner and much more easier to update if you are adding more events with different texts.
and yes the FireAllClients() will fire to the clients at the same time so you dont have to worry about the clients seeing same message at same time.

1 Like
local PG = game.Players.LocalPlayer:WaitForChild('PlayerGui');

Why do you do WaitForChild when the PlayerGui should automatically be loaded? I recommand doing:

local PG = game.Players.LocalPlayer.PlayerGui;
1 Like

Do not use, ; because It’s used for different purposes not this.

Guess I’ll give an explanation, the reason why it’s only working for the Run button is because you’re changing it from the server’s perspective

Think of it like this: The Server & the Client are 2 different things, like comparing an Apple to an Orange; they aren’t the same

  • The Client’s GUI is the PlayerGUI

  • The Server’s GUI is the StarterGUI (Which is then replicated to all players when they join, but it won’t update unless ResetOnSpawn is enabled)

I am aware that people have found a solution to this but To fix it, you could create a RemoteEvent that can transfer server-client wise using the FireAllClients() function, using that in mind here:

----Server Script

--Variables
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

local sprinklers = game.Workspace.Sprinklers
local check = true
local S1V = sprinklers.Sprinkler1.Water.ParticleEmitter.Transparency
local S2V = sprinklers.Sprinkler2.Water.ParticleEmitter.Transparency
local S3V = sprinklers.Sprinkler3.Water.ParticleEmitter.Transparency
local S4V = sprinklers.Sprinkler4.Water.ParticleEmitter.Transparency
local S5V = sprinklers.Sprinkler5.Water.ParticleEmitter.Transparency
local S6V = sprinklers.Sprinkler6.Water.ParticleEmitter.Transparency
local S7V = sprinklers.Sprinkler7.Water.ParticleEmitter.Transparency
local S8V = sprinklers.Sprinkler8.Water.ParticleEmitter.Transparency
local S9V = sprinklers.Sprinkler9.Water.ParticleEmitter.Transparency

--Loops

for countdown = 10, 0, -1 do
    game.StarterGui.ScreenGui.TextLabel.Text = "Safe. Next event will occur in " .. countdown .. " seconds" --If you want the Gui to change whenever a player joins late in-game, you can keep this
    Event:FireAllClients("Safe. Next event will occur in " .. countdown .. " seconds")
    wait(1)

    if countdown == 0 then
    	local event = math.random(1,2)
    	if event == 1 then
	    	game.StarterGui.ScreenGui.TextLabel.Text = "Flood! Get to the high point of the castle!" 
            Event:FireAllClients("Flood! Get to the high point of the castle!" ) --This basically just fires to all available players that are currently ingame
	    	S1V = 0
	    	S2V = 0
	    	S3V = 0
	    	S4V = 0
	    	S5V = 0
	    	S6V = 0
    		S7V = 0
	    	S8V = 0
	    	S9V = 0
	    elseif event == 2 then
	    	game.StarterGui.ScreenGui.TextLabel.Text = "Lawnmower! Get to the cave!"
            Event:FireAllClients("Lawnmower! Get to the cave!") --This basically just fires to all available players that are currently ingame
		end
	end
end
--LocalScript
local Player = game.Players.LocalPLayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")

RemoteEvent.OnClientEvent:Connect(function(TextChange)
    PlayerGui.ScreenGui.TextLabel.Text = TextChange
end)

Sometimes the Gui will not load entirely in relation to the server, which would result in an error: PlayerGui is not a valid member of Player, which is why we use WaitForChild() (Try it yourself)

local Player = game.Players.LocalPlayer
local PlayerGui = Player.PlayerGui

So I add the serverscript under workspace and the local script in the same place? Also since there ARE going to be more events than just the text changing, say raising a part multiple times, what would I do in order to make it happen at the same time like everything else?

ServerScript should be inside ServerScriptService, it could be inside workspace but i dont recommend it honestly. LocalScript should be inside your StarterGui/StarterPack/ StarterPlayerScripts/ StarterCharacterScripts/or your ScreenUI. I recommend you putting it inside your screenUI though cause this localscript only interacts with your screenui and nothing else

ok Thanks. I am trying it now. Sorry I couldn’t do it before, my schedule is packed with school and I do not have much room for doing this at the moment.

Try to use a,

for i,v in pairs(sprinklers) do

end