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

Roblox U game loop, help?

local checkpoint1 = game.Workspace.Checkpoint1
local checkpoint2 = game.Workspace.Checkpoint2
local checkpoint3 = game.Workspace.Checkpoint3
local show = false
local oldcars = game.ServerStorage:GetChildren()

local raceInProgress = false

function showVictoryMessage(playerName)
	if show == false then
		show = true
		local message = Instance.new("Message")
		message.Text = playerName .. " wins!"
		message.Parent = game.Workspace
		wait(2)
		message:Destroy()
		show = false
	end
end	
	

function destroyCars()
	if raceInProgress == true then	
		for _, object in pairs (game.Workspace:GetChildren()) do
			print(object.Name)
			if object.Name == "Car" then
				print("This is a car, we need to destroy it!")
				object:Destroy()
			end
		end	
	end
end

function createCars()
	for _, object in pairs(game.ServerStorage:GetChildren()) do 	
		local oldcar = object:Clone()
		oldcar.Parent = game.Workspace
		oldcar:MakeJoints()
		
	end
end

function CreateCarsV2()
	for _, object in pairs (game.ServerStorage:GetChildren()) do
		local carClone = object:Clone()
		if carClone and carClone.Parent == game.ServerStorage then
			carClone.Parent = game.Workspace
			carClone:MakeJoints()
		end
	end
end

function checkpoint1hit(otherPart)
	print("Checkpoint1 was hit!")
	print(otherPart.Name)
	if otherPart ~= nil and otherPart.Parent ~= nil and otherPart.Parent:FindFirstChild("Humanoid") then
		print("A player hit me!")
		if not checkpoint1:FindFirstChild(otherPart.Parent.Name) then
			local playerTag = Instance.new("StringValue")
			playerTag.Parent = checkpoint1
			playerTag.Name = otherPart.Parent.Name
			playerTag.Value = otherPart.Parent.Name
		end
		
		if checkpoint3:FindFirstChild(otherPart.Parent.Name) and raceInProgress == true then
			print("Player wins")
			
			showVictoryMessage(otherPart.Parent.Name)
			raceInProgress = false
		end		
		
	end
end

function checkpoint2hit(otherPart)
	print("Checkpoint2 was hit!")
	print(otherPart.Name)
	if otherPart ~= nil and otherPart.Parent ~= nil and otherPart.Parent:FindFirstChild("Humanoid") then
		print("A player hit me!")
		if not checkpoint2:FindFirstChild(otherPart.Parent.Name) and checkpoint1:FindFirstChild(otherPart.Parent.Name) then
			local playerTag = Instance.new("StringValue")
			playerTag.Parent = checkpoint2
			playerTag.Name = otherPart.Parent.Name
			playerTag.Value = otherPart.Parent.Name
		end
	end
end

function checkpoint3hit(otherPart)
	print("Checkpoint1 was hit!")
	print(otherPart.Name)
	if otherPart ~= nil and otherPart.Parent ~= nil and otherPart.Parent:FindFirstChild("Humanoid") then
		print("A player hit me!")
		if not checkpoint3:FindFirstChild(otherPart.Parent.Name) and checkpoint2:FindFirstChild(otherPart.Parent.Name) then
			local playerTag = Instance.new("StringValue")
			playerTag.Parent = checkpoint3
			playerTag.Name = otherPart.Parent.Name
			playerTag.Value = otherPart.Parent.Name
		end
	end
end

checkpoint1.Touched:connect(checkpoint1hit)
checkpoint2.Touched:connect(checkpoint2hit)
checkpoint3.Touched:connect(checkpoint3hit)


local redLight = game.Workspace.StartLight.RedModel.LightBulb.PointLight
local yellowLight = game.Workspace.StartLight.YellowModel.LightBulb.PointLight
local greenLight = game.Workspace.StartLight.GreenModel.LightBulb.PointLight
local startBarrier = game.Workspace.StartBarrier

function startLightCycle()
	wait(5)

	redLight.Enabled = true
	
	wait(1)
	
	redLight.Enabled = false
	yellowLight.Enabled = true
	
	wait(1)
	
	yellowLight.Enabled = false
	greenLight.Enabled = true
end

function removeBarrier()
	startBarrier.Transparency = 1
	startBarrier.CanCollide = false
end

function resetLights()
	redLight.Enabled = false
	yellowLight.Enabled = false
	greenLight.Enabled = false
end

function resetBarrier()
	startBarrier.Transparency = 0.5
	startBarrier.CanCollide = true
end

function clearCheckpoint(checkpoint)
	for _, object in pairs(checkpoint:GetChildren()) do
		if object.Name ~= "TouchInterest" then
			object:Destroy()
		end	
	end
end

wait(10)

while true do
	--setup racetrack
	--turn off all lights
	resetLights()
	--reset barrier
	resetBarrier()
	
	--destroy cars
	destroyCars()
	
	--create cars	
	createCars()
	
	--clear all checkpoints
	clearCheckpoint(checkpoint1)
	clearCheckpoint(checkpoint2)
	clearCheckpoint(checkpoint3)
		
	--start race
	--start the light cycle
	startLightCycle()
	--remove barrier
	removeBarrier()
	
	raceInProgress = false
	--wait for race to finish
	while raceInProgress == true do
		wait()
	end	
	
	wait(10)
end

I watched the video and I copied the code character to character but it before the race is over, it will start the game loop from the beginning. It would make new cars and delete cars while the race is still in progress.

1 answer

✓ Accepted answer

Try commenting out line 179 as shown below.

   --raceInProgress = false -- THIS IS LINE 179 - COMMENT THIS OUT
    --wait for race to finish
    while raceInProgress == true do
        wait()
    end

It looks like you might have added this - this value shouldn't be changed to false unless the 3rd checkpoint1hit event function is run, but of course it should be initialized at the top, which it is.

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