How do I check for an event and a game loop at the same time?

What do you want to achieve?
I am making a simple obby game with a game loop, where you have to complete an obby before the time ends (game loop), but I dont know how to have the game loop running (the “for i = 30, 1, -1 do” loop) while checking for the touched event from the end of the obby part at the same time.
How do I check for the event while having the game loop running? If possible, I’d like to do it in 1 script, so i can access the players and characters in my tables.

2 Likes

I recommend using “RunService”.
As you can do it all in one script if thats what you’re looking for.
More into it : RunService | Roblox Creator Documentation

here is a simpler solution this script should work by only having a function for detecting the player if he reaches the end and a loop for the timer:

local Win = false --this variable is to tell the loop to stop when the player reaches the end


YourDetectionPart.Touched:Connect(function(hit)
	if hit.Parent:GetChildren("Humanoid") then--To be sure that the part got touched by a player
		Win = true
	end

end)



for i = 30,1,-1 do
	print(i)
	if Win == true then--if the win variable = true then we just stop the timer
		print("The Player Reached The end!")
		break--Stops the loop
	end
	wait(1)
end
if Win == false then--if the timer ended and the win variable is still = to false then it means that the player failed.
	print("the player failed to Reach the end in time")
end

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