Script should be firing for everyone in server, yet only works for 1 player

Basically, I have this script that detects when a value = 1, then displays dialogue.
For some reason, the dialogue only shows for 1 person in the server.

Here’s the script:

wait(1)

wavenum = workspace.StatValues.WaveNum
sound = script.Parent.Sound
deep = script.Parent.DeepTalk

stuff = true

while stuff == true do
	wait(0.2)
	
	if wavenum.Value == 1 then
		
		local players = game:GetService("Players"):GetPlayers()
		for i, v in pairs(players) do
			
			v.PlayerGui.Dialogue.Enabled = true
			v.PlayerGui.Dialogue.Frame.TextLabel.Text = "Hello, Raig's!"
			sound:Play()
			wait(3)
			v.PlayerGui.Dialogue.Frame.TextLabel.Text = "Unfortunately, with your betrayal, we must invade your base and take down that raig core."
			sound:Play()
			wait(5)
			v.PlayerGui.Dialogue.Frame.TextLabel.Text = "So, I will send a few units to go destroy the core! Thanks!"
			sound:Play()
			wait(5)
			v.PlayerGui.Dialogue.Enabled = false
			
			stuff = false
			
			script:Destroy()
			
			break
			
		end
	end
end

The script is in a folder inside of workspace, and it’s a normal script, not local.

The other players might be joining after this loop runs.

I tested it with that 2 player thing, I change the wave to 1 and only 1 of the players gets the dialogue. (2 player thing being when you go to Test, then Start)

Oh, it’s because you have a break and a script:Destroy(). Get rid of those and it should work.

I deleted them, nothing changed. Proof:

wait(1)

wavenum = workspace.StatValues.WaveNum
sound = script.Parent.Sound
deep = script.Parent.DeepTalk

stuff = true

while stuff == true do
	wait(0.2)
	
	if wavenum.Value == 1 then
		
		local players = game:GetService("Players"):GetPlayers()
		for i, v in pairs(players) do
			
			v.PlayerGui.Dialogue.Enabled = true
			v.PlayerGui.Dialogue.Frame.TextLabel.Text = "Hello, Raig's!"
			sound:Play()
			wait(3)
			v.PlayerGui.Dialogue.Frame.TextLabel.Text = "Unfortunately, with your betrayal, we must invade your base and take down that raig core."
			sound:Play()
			wait(5)
			v.PlayerGui.Dialogue.Frame.TextLabel.Text = "So, I will send a few units to go destroy the core! Thanks!"
			sound:Play()
			wait(5)
			v.PlayerGui.Dialogue.Enabled = false
			
			stuff = false
			
		end
	end
end

Ohhhh, your waits are making the loop wait until the first player’s animation finishes before it moves on to the next. Use task.spawn so it runs it on a new “thread”.

Code:

wait(1)

local wavenum = workspace.StatValues.WaveNum
local sound = script.Parent.Sound
local deep = script.Parent.DeepTalk

local stuff = true

while stuff == true do
	wait(0.2)

	if wavenum.Value == 1 then
		for i, v in game:GetService("Players"):GetPlayers() do
			task.spawn(function()
				v.PlayerGui.Dialogue.Enabled = true
				v.PlayerGui.Dialogue.Frame.TextLabel.Text = "Hello, Raig's!"
				sound:Play()
				wait(3)
				v.PlayerGui.Dialogue.Frame.TextLabel.Text = "Unfortunately, with your betrayal, we must invade your base and take down that raig core."
				sound:Play()
				wait(5)
				v.PlayerGui.Dialogue.Frame.TextLabel.Text = "So, I will send a few units to go destroy the core! Thanks!"
				sound:Play()
				wait(5)
				v.PlayerGui.Dialogue.Enabled = false
			end)
			
			stuff = false
		end
	end
end

You should also be using task.wait instead of wait, here’s why.

1 Like

It actually worked!! Thank you!!!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.