Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

While true loop works first time but not another?

So, I have a while true loop and part of it, is this:

while true do
	MScreen = game.Lighting.MurderScreen:Clone()
	MScreen.Parent = V.PlayerGui
	repeat wait() until MScreen.Parent == V.PlayerGui
	MScreen.Name = "MurderScreen"
	wait()
	repeat wait() print("Waiting") until MScreen:FindFirstChild("MurderType")  -- ERROR LINE
	wait(10)
end

whenever it goes a second time, it can never find MurderType but it always works the first time

Full script: (error at line 150)

if Re >= 2 then
		wait()
	while true do
		wait()
		local R = game.Workspace.InGame:GetChildren()
	local Re = #R
		if Re >= 2 then
	local pLayers = game.Workspace.InGame:GetChildren()
	if #pLayers ~= 0 then
	Picking.Visible = false
	Playeri.Visible = true
	Days.Visible = true
	Playeri.Text = "Players Left: " .. #pLayers
	local Time = game.Lighting.ClockTime
	Time = 12
	while true do
		if Time >= 20 then break end
		if game.Workspace.Reported.Value == true then break end
	game.Lighting:SetMinutesAfterMidnight(Time*60)
	wait(0.1)
	Time = Time + 0.05
	print(Time)
	end
	if game.Workspace.Reported.Value == true then 
		game.Lighting.ClockTime = 12
		for i,v in pairs(game.Players:GetChild()) do
			if v.Winner.Value == true then
				Winner = v.Name
			end
		end
		Days.Visible = false
		Playeri.Visible = false
		Picking.Text = Winner .. " found out who the murderer was"
		Picking.Visible = true
		wait(3)
		Picking.Text = "It was..."
		wait(3)
		Picking.Text = game.Workspace.Murderer.Value
		wait(2)
		game.Workspace.CoinGiver.Disabled = false
		repeat wait() until game.Workspace.CoinGiver.Disabled == false
		Days.Visible = false
		Playeri.Visible = false
		Picking.Text = "Everyone Now Gets Coins!!"
		Picking.Visible = true
		wait(3)
		break
	end
	if #IN >= 1 then 
		for i,v in pairs(game.Workspace.DeadBody:GetChildren())do
			v:Remove()
		end
	end
	for i,v in pairs(game.Workspace.InGame:GetChildren()) do
		local V = game.Players:FindFirstChild(v.Name)
			V.PlayerGui.Black.Frame.In.Disabled = false
			V.PlayerGui.Black.Frame.Out.Disabled = true
			wait(5)
		if v.Name == game.Workspace.Murderer.Value then
			print("Found")
			local V = game.Players:FindFirstChild(v.Name)
			MScreen = game.Lighting.MurderScreen:Clone()
			MScreen.Parent = V.PlayerGui
			repeat wait() until MScreen.Parent == V.PlayerGui
			MScreen.Name = "MurderScreen"
			local Players = game.Workspace.InGame:GetChildren()
			local Ran1 = math.random(1,#Players)
			local P1 = Players[Ran1]
			local Ran2 = math.random(1,#Players)
			local P2 = Players[Ran2]
			local Ran3 = math.random(1,#Players)
			local P3 = Players[Ran3]
			wait(10)
			if P1.Name == game.Workspace.Murderer.Value then
				while true do
					wait()
					local Ran1 = math.random(1,#Players)
					local P1 = Players[Ran1]
					if P1.Name ~= game.Workspace.Murderer.Value then MScreen.Players.Player1.Text = P1.Name 
					MScreen.Players.Player1.PlayerName.Value = P1.Name break end
				end
				else
			MScreen.Players.Player1.Text = P1.Name
			MScreen.Players.Player1.PlayerName.Value = P1.Name
			end
			if P2.Name == game.Workspace.Murderer.Value or P1.Name then
			while true do
					if #Players <= 2 then MScreen.Players.Player2.Text = "Eliminated" break end
					wait()
					local Ran2 = math.random(1,#Players)
					local P2 = Players[Ran2]
					if P2.Name ~= game.Workspace.Murderer.Value and P2.Name then MScreen.Players.Player2.Text = P2.Name 
					MScreen.Players.Player1.PlayerName.Value = P1.Name break end
					end
			else
			MScreen.Players.Player2.Text = P2.Name
			MScreen.Players.Player2.PlayerName.Value = P2.Name
			end
			if P3.Name == game.Workspace.Murderer.Value or P2.Name then
			while true do
				if #Players <= 3 then MScreen.Players.Player3.Text = "Eliminated" break end
					wait()
					local Ran3 = math.random(1,#Players)
					local P3 = Players[Ran2]
					if P3.Name ~= game.Workspace.Murderer.Value and P3.Name then MScreen.Players.Player3.Text = P3.Name 
					MScreen.Players.Player1.PlayerName.Value = P1.Name break end
				end
				else
			MScreen.Players.Player3.Text = P1.Name
			MScreen.Players.Player3.PlayerName.Value = P3.Name
			end
			local Murders = game.Lighting.KillingWays:GetChildren()
			local Ran1 = math.random(1,#Murders)
			M1 = Murders[Ran1]
			if #Murders >= 2 then
			local Ran2 = math.random(1,#Murders)
			M2 = Murders[Ran2]
			if #Murders >= 3 then
			local Ran3 = math.random(1,#Murders)
			M3 = Murders[Ran3]
			end
			end
			MScreen.MurderType.Option1.Text = M1.Value
			MScreen.MurderType.Option1.Type.Value = M1.Name
			print(M1.Value)
			if M2 ~= nil then
			if M2.Value == M1.Value then
			for i,v in pairs(game.Lighting.KillingWays:GetChildren()) do
					if v.Value ~= M1.Value then 
					M2 = v
end
				end
			end
			MScreen.MurderType.Option2.Text = M2.Value
			MScreen.MurderType.Option2.Type.Value = M2.Name
			end
			end
			if M3 ~= nil then
				print("VAUT")
			if M3.Value == M1.Value then
			for i,v in pairs(game.Lighting.KillingWays:GetChildren()) do
					if v.Value ~= M1.Value then
						if v.Value ~= M2.Value then
					M3.Value = v.Value
					print(M3.Value)
						end
					end
					end
			end
			repeat wait() print("Waiting") until MScreen:FindFirstChild("MurderType") 
                                                 --ERROR LINE ABOVE
			local MurderType = MScreen:WaitForChild("MurderType")
			MScreen:WaitForChild("MurderType").Option3.Text = M3.Value
			MScreen.MurderType.Option3.Type.Value = M3.Name
			if M3.Value == M2.Value then
			for i,v in pairs(game.Lighting.KillingWays:GetChildren()) do
					if v.Value ~= M1.Value then
						if v.Value ~= M2.Value then
					M3.Value = v.Value
						end
					end
					end
			end
			
			MurderType.Option3.Text = M3.Value
			MurderType.Option3.Type.Value = M3.Name
			end
			local MScreen = game.Players:FindFirstChild(game.Workspace.Murderer.Value).PlayerGui:WaitForChild("MurderScreen")
	end	
			MScreen.MurderType.Option1.Script.Disabled = false	
			MScreen.MurderType.Option2.Script.Disabled = false
			MScreen.MurderType.Option3.Script.Disabled = false		
			MScreen.Players.Player1.Script.Disabled = false
			MScreen.Players.Player2.Script.Disabled = false	
			MScreen.Players.Player3.Script.Disabled = false
			wait()	
			MScreen.Enabled = true
			while true do 
				wait()
				print("Waiting")
				if MScreen.Enabled == false then break end
			end
			MScreen:Remove()
			wait()
			print("Done")
			local IN = game.Workspace.DeadBody:GetChildren()
			for i,v in pairs(game.Workspace.InGame:GetChildren()) do
				local V = game.Players:FindFirstChild(v.Name)
				V.PlayerGui.Black.Frame.In.Disabled = true
				V.PlayerGui.Black.Frame.Out.Disabled = false
			end
		end 
		else
			print("Disabled")
		game.Workspace.CoinGiver.Disabled = false
		repeat wait() until game.Workspace.CoinGiver.Disabled == false
		Days.Visible = false
		Playeri.Visible = false
		Picking.Text = "Giving Players Coins"
		Picking.Visible = true
		wait(3)
		break
		end
	end
		
		end

3 answers

✓ Accepted answer

I don't exactly know what your error might be, but try the following:

Perhaps "MurderType" gets renamed or deleted/moved during the script. Try to add


print(MScreen:FindFirstChild("MurderType"))

If this prints "nil", then that is the problem. Or, put a breakpoint at line 150, and when the script stops at the breakpoint, look at the children in "MScreen"

Also, does it print an error message, or does it just keep waiting at the line 150?

Accept if this helped

You cannot have multiple infinite loops in a single script.

while true do
	wait()
end

is an infinite loop, if you really need multiple infinite loops in a single script I'd recommend coroutines.

You cannot do

wait()

Add a number to it Recommended for you wait(0.5)

Get back to me ASAP if it works or not

2 comments

0

yes you can do wait()

0

ya you can but after some more testing i figured out that once the script repeats it causes lag so you need to do waitforchild and it fixes it but thanks for feedback

This discussion is preserved as it was. New answers, comments, and votes are closed.