Confused on why GUI isn't popping up when all parts are changed to Bright red via touched event

So I’m making a script that changes the brick color of a random part in the model to Bright red. The part will then be parented to the workspace. When all parts are in the workspace and bright red I would like the GUI to pop up. So far the bricks turn red and are parented to the workspace but the gui is not popping up.

local debounce = false
local release = game.Workspace.Release
local releaseParts = release:GetChildren()
local Players = game:GetService("Players")
local allPlayers = Players:GetPlayers()

local function colorBricksRed(part, object)
	if not debounce then
		debounce = true
		if object:FindFirstChild("Humanoid")then
			local randomPart = releaseParts[math.random(1, #releaseParts)]
			randomPart.BrickColor = BrickColor.new("Bright red")
			randomPart.Parent = game.Workspace
			releaseParts = release:GetChildren()
		end
		wait(1)
		debounce = false
	end
end

local function openGuiWithParts()
	for i, v in pairs(allPlayers) do
                v.PlayerGui:WaitForChild("ScreenGui")
		v.PlayerGui.ScreenGui.Enabled = true
	end
end

for i, v in pairs(releaseParts)do
	v.Touched:Connect(function(part)
		if not debounce then
			colorBricksRed(v, part.Parent)
		end
	end)
end

while true do
	if game.Workspace:FindFirstChild("Part1")and game.Workspace.Part1.BrickColor == ("Bright red") then
		if game.Workspace:FindFirstChild("Part2")and game.Workspace.Part2.BrickColor == ("Bright red") then
			if game.Workspace:FindFirstChild("Part3")and game.Workspace.Part3.BrickColor == ("Bright red") then
				if game.Workspace:FindFirstChild("Part4")and game.Workspace.Part4.BrickColor == ("Bright red") then
					openGuiWithParts()
				end
			end
		end
	end
	wait(1)
end

1 Like

Did you misspell PlayerGui in the actual script?

Oh boy, that’s embarrassing. I did, but the issue still persists.

Maybe it’s a problem with my consecutive if statements? I am not sure, still having this issue.

Can you add print statements to see what gets printed?

1 Like

I attempted that but it won’t go deeper than the initial “while true do” loop.

What @NineFineMuscadines said, it won’t go past the initial while true do loop. I’m not sure why that is though, Part1 does eventually get added to the workspace after it becomes red?

Wait, what are you even doing with this…? Shouldn’t you make it a Variable or something?

Also I think you may need to do BrickColor.new("Bright red") instead

1 Like

Okay, so the while loop works now, but what do you mean by making it a variable? The Screen Gui still doesn’t pop up.

Nevermind what I just said aaa My guess is that you’re only getting the Players once, either that or 1 of your conditional if statements aren’t equal to the BrickColor it’s supposed to be

1 Like

I tried to do it like this instead.

local debounce = false
local release = game.Workspace.Release
local releaseParts = release:GetChildren()
local Players = game:GetService("Players")
local allPlayers = Players:GetPlayers()

local function colorBricksRed(part, object)
	if not debounce then
		debounce = true
		if object:FindFirstChild("Humanoid")then
			local randomPart = releaseParts[math.random(1, #releaseParts)]
			randomPart.BrickColor = BrickColor.new("Bright red")
			randomPart.Parent = game.Workspace
			releaseParts = release:GetChildren()
		end
		wait(1)
		debounce = false
	end
end

for i, v in pairs(releaseParts)do
	v.Touched:Connect(function(part)
		if not debounce then
			colorBricksRed(v, part.Parent)
		end
	end)
end

while true do
	if game.Workspace:FindFirstChild("Part1")and game.Workspace.Part1.BrickColor == BrickColor.new("Bright red") then
		if game.Workspace:FindFirstChild("Part2")and game.Workspace.Part2.BrickColor == BrickColor.new("Bright red") then
			if game.Workspace:FindFirstChild("Part3")and game.Workspace.Part3.BrickColor == BrickColor.new("Bright red") then
				if game.Workspace:FindFirstChild("Part4")and game.Workspace.Part4.BrickColor == BrickColor.new("Bright red") then
					for i, v in pairs(allPlayers)do
						if v.PlayerGui.ScreenGui.Enabled == false then
							v.Player.ScreenGui.Enabled = true
						elseif v.PlayerGui.ScreenGui.Enabled == true then
							break
						end
					end
				end
			end
		end
	end
	wait(1)
end

It still didn’t work, rip.

Try doing this instead?

for i, v in pairs(game.Players:GetPlayers()) do
	if v.PlayerGui.ScreenGui.Enabled == false then
		v.Player.ScreenGui.Enabled = true
	elseif v.PlayerGui.ScreenGui.Enabled == true then
		break
	end
end
1 Like

Bringo as Dr. Steve Brule would say! I’ve had this issue before, but I forgot about it. I wonder why this happens, don’t they mean the same thing?

I’ll actually explain it
So, from what I understand, turns out the Script actually starts to already get all the Players when it first runs, but since you defined your allplayers variable at the start, my guess is that the script already detects no players when it first runs inside the server (Since it runs too quickly that it doesn’t detect you while you hit Play)

You can also try out this code for future reference I guess inside the workspace:

local Players = #game.Players:GetPlayers()
print(Players)
--Expected Output: 0
1 Like