Help with on server event script needed

Hello,
I am trying to figure out how to fix my script. The script below is a server script that should show gui to everyone in the server.
Issue: The script works but it is very slow. - It takes very long to show the gui to everyone in the server. (The times of the gui shows for each player are different)

game.ReplicatedStorage.Vote.OnServerEvent:Connect(function(Player, vote)
	for i,plr in pairs(game:GetService("Players"):GetPlayers())do
		plr.PlayerGui:WaitForChild("ScreenGui").Frame.Visible = true
		game.Workspace:WaitForChild("Sounds").Sound:Play()
		wait(5)
			plr.PlayerGui:WaitForChild("ScreenGui").Frame.Visible = false
	end
	game.Workspace.Part.Material = "Neon"
	game.Workspace:WaitForChild("Sounds").Sound2:Play()

Thanks

It’s slow because you have a wait(5) in a loop, meaning before it get’s to the next player, it will wait 5 seconds. If you really want to keep this as a single loop, rather than making another loop to change the visibility to false, try the following implementation instead.

game.ReplicatedStorage.Vote.OnServerEvent:Connect(function(Player, vote)
	
	for i, plr in pairs(game.Players:GetPlayers()) do
		plr.PlayerGui:WaitForChild("ScreenGui").Frame.Visible = true
		game.Workspace:WaitForChild("Sounds").Sound:Play()
		spawn(function()
			task.wait(5)
			plr.PlayerGui:WaitForChild("ScreenGui").Frame.Visible = false
		end)
	end
	game.Workspace.Part.Material = "Neon"
	game.Workspace:WaitForChild("Sounds").Sound2:Play()
	
end)

Alternatively you could use two loops as well, this is just the least disruptive implementation of what you want without bloating the code you already provided. I’d recommend exploring other methods.

1 Like

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