How to stop the round when a player touches a part?

I am making a marble game, and I want it so whenever a player touches a part, the round ends. Can anyone help me with that? Thank you;

Main Script:

local roundLength = 10 -- usually 60
local intermissionLength = 10 -- usually 25

local InRound = game.ReplicatedStorage.InRound
local Status = game.ReplicatedStorage.Status

local MapsFolder = game.ReplicatedStorage.Maps

local LobbySpawn = game.Workspace.Spawn
--[[
local function ToMinutesAndSeconds(seconds)
	
	return Format(math.floor(seconds / 60))..":"..Format(seconds % 60)
	
end
]]
InRound.Changed:Connect(function()
	
	if InRound.Value == true then

		local mapChildren = MapsFolder:GetChildren()
		local chosenMap = mapChildren[math.random(1,#mapChildren)]
		clonedMap = chosenMap:Clone()
		clonedMap.Parent = game.Workspace.WorkspaceMaps
		
		for _, player in pairs(game.Players:GetPlayers()) do
			
			local char = player.Character
			char.HumanoidRootPart.CFrame = clonedMap.Spawn.CFrame
			
		end
	else
		for _, player in pairs(game.Players:GetPlayers()) do
			
			local char = player.Character
			char.HumanoidRootPart.CFrame = LobbySpawn.CFrame
			
		end
		
		clonedMap:Destroy()
	end

end)

function Format(Int)
	return string.format("%02i", Int)
end

function convertToMS(Seconds)
	local Minutes = (Seconds - Seconds%60)/60
	Seconds = Seconds - Minutes*60
	return Format(Minutes)..":"..Format(Seconds)
end

local function startRoundTimer()
	while wait() do
		for i = intermissionLength,1,-1 do
			InRound.Value = false
			Status.Value = "Intermission: "..convertToMS(i).." until a new round."
			task.wait(1)
		end	
		
		for i = roundLength,1,-1 do
			InRound.Value = true
			if #workspace.WorkspaceMaps:GetChildren() > 0 then
				
				Status.Value = "Ingame: "..convertToMS(i).." until round is over. Map Chosen: "..workspace.WorkspaceMaps:GetChildren()[1].Name
				
			else
				
				Status.Value = "Ingame: "..convertToMS(i).." until round is over."
				
			end
			
			task.wait(1)
			
			
		end
	
	end
end

task.spawn(startRoundTimer)

Thank you again for looking.

1 Like

You can make a variable for it to check for. Like: local timerRun = false and then when you run the timer put the variable to true and then make a new function where you do timerRun = false and reset the time variable. Don’t forget to put in the while loop where the timer runs like: while timerRun do. And also don’t forget to put change the timerRun variable to true in the startRoundTimer before you run the while loop.

Shouldn’t it be a global variable?

No I don’t think that is necassary. As long as you just make a stopTimer function or something for it then it should still work. Just put the timerRun at the top of the script.

Yes, but I am running the Touched Event from a different script though

Well then you could put the timer script in a ModuleScript so you could require it and call the stopTimer function from another script. Alternatively you could make a boolValue somewhere called something like ‘timerRunning’ and then you could use that to check for in both scripts.

Can you provide an example script, since I tried before and had no success?

Do you want an example of the ModuleScript or the value?

Nevermind, I think I just fixed it with just 1 line of code.

I appreciate you helping!

And if you guys were wondering how, here is the code:

if not InRound.Value and (i ~= roundLength) then break end