How would I loop through everything in a table at once

So currently I’m trying to achieve this effect of darkening the screen of players that are in a certain table. However, using in pairs only lets me do this to one player at one time, how would I be able to do it to all the players in a table at the same time? I haven’t tried anything else as I couldn’t think of anything else that could work.

sample code

players = game.Players:GetPlayers()
for i,v in pairs (players) do
	local blackscreen = v.PlayerGui.Frame
		for i = 1,10 do wait(.5)
			blackscreen.Transparency = blackscreen.Transparency + 0.1
		end	
end

Use tween service, this won’t require another loop inside the player loop

local TweenService = game:GetService("TweenService")
local TweenSettings = TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
local Players = game:GetService("Players")
for i,v in ipairs(Players:GetPlayers()) do
    local blackscreen = v:WaitForChild("BlackScreen")
    local Tween = TweenService:Create(blackscreen, TweenSettings, {Transparency = 1})
    Tween:Play()
end

However, I’d just like to point out that handling UI effects on the server is not a good idea. I suggest having a remote event to fire all clients and let the clients do the tweening like this:

--Server
local RemoteEvent = ReplicatedStorage.RemoteEvent
RemoteEvent:FireAllClients()
--Local Script
local RemoteEvent = ReplicatedStorage.RemoteEvent
RemoteEvent.OnClientEvent:Connect(function()
    local blackscreen = LocalPathToScreen
    local Tween = TweenService:Create(blackscreen, TweenSettings, {Transparency = 1})
    Tween:Play()
end
3 Likes

I would also recommend the TweenService. Your transitions are going to be much cleaner and there are a variety of settings that you can also change.

Also, I am not sure where BlackScreen is. If it is in the PlayerGui, I would recommend having a remote event that fires and tells each client to darken their screen. The server should not be controlling Player GUI’s directly.

Thanks, it worked fine! :grinning:

While TweenService is the best option here, it may not be an option in similar situations. If you wanted to do it the way you did in the OP, you would put the inner for loop inside a spawn(function() [code] end) thread.

1 Like