How to cancel a RemoteEvent using a RemoteEvent

Hello!

Ok so, I am trying to make a timer system for a daylight cycle in my simulator game. The way it works it that there will be a max time, in this case 15 minutes, and when the timer ends, it will become night/day. Continuing, I want the player to be able to cancel it if they’re in a private server (they will have a button that will cancel the timer).

I’ve made the TimerScript which runs off the DayTimeEvent.

DayTimeEventScript- In StarterCharacterScripts and it’s a LocalScript
TimerCancelScript - In StarterCharacterScripts and it’s a LocalScript

Here’s the DayTimeEventScript:

local ReplicatedStorage = game.ReplicatedStorage
local RemoteEvents = ReplicatedStorage.RemoteEvents
local CycleDayEvent = RemoteEvents.CycleDayEvent
local UI = game.Players.LocalPlayer.PlayerGui.Main
local TextLabel = UI.TextLabel
local Text = TextLabel.Text

local function CycleTimer()
	TextLabel.Visible = true
	Text = "15 Minutes"
	wait(60)
	Text = "14 Minutes"
	wait(60)
	Text = "13 Minutes"
	wait(60)
	Text = "12 Minutes"
	wait(60)
	Text = "11 Minutes"
	wait(60)
	Text = "10 Minutes"
	wait(60)
	Text = "9 Minutes"
	wait(60)
	Text = "8 Minutes"
	wait(60)
	Text = "7 Minutes"
	wait(60)
	Text = "6 Minutes"
	wait(60)
	Text = "5 Minutes"
	wait(60)
	Text = "4 Minutes"
	wait(60)
	Text = "3 Minutes"
	wait(60)
	Text = "2 Minutes"
	wait(60)
	Text = "1:00"
	wait(1)
	Text = "00:59"
	wait(1)
	Text = "00:58"
	wait(1)
	Text = "00:57"
	wait(1)
	Text = "00:56"
	wait(1)
	Text = "00:55"
	wait(1)
	Text = "00:54"
	wait(1)
	Text = "00:53"
	wait(1)
	Text = "00:52"
	wait(1)
	Text = "00:51"
	wait(1)
	Text = "00:50"
	wait(1)
	Text = "00:49"
	wait(1)
	Text = "00:48"
	wait(1)
	Text = "00:47"
	wait(1)
	Text = "00:46"
	wait(1)
	Text = "00:45"
	wait(1)
	Text = "00:44"
	wait(1)
	Text = "00:43"
	wait(1)
	Text = "00:42"
	wait(1)
	Text = "00:41"
	wait(1)
	Text = "00:40"
	wait(1)
	Text = "00:39"
	wait(1)
	Text = "00:38"
	wait(1)
	Text = "00:37"
	wait(1)
	Text = "00:36"
	wait(1)
	Text = "00:35"
	wait(1)
	Text = "00:34"
	wait(1)
	Text = "00:33"
	wait(1)
	Text = "00:32"
	wait(1)
	Text = "00:31"
	wait(1)
	Text = "00:30"
	wait(1)
	Text = "00:29"
	wait(1)
	Text = "00:28"
	wait(1)
	Text = "00:27"
	wait(1)
	Text = "00:26"
	wait(1)
	Text = "00:25"
	wait(1)
	Text = "00:24"
	wait(1)
	Text = "00:23"
	wait(1)
	Text = "00:22"
	wait(1)
	Text = "00:21"
	wait(1)
	Text = "00:20"
	wait(1)
	Text = "00:19"
	wait(1)
	Text = "00:18"
	wait(1)
	Text = "00:17"
	wait(1)
	Text = "00:16"
	wait(1)
	Text = "00:15"
	wait(1)
	Text = "00:14"
	wait(1)
	Text = "00:13"
	wait(1)
	Text = "00:12"
	wait(1)
	Text = "00:11"
	wait(1)
	Text = "00:10"
	wait(1)
	Text = "00:09"
	wait(1)
	Text = "00:08"
	wait(1)
	Text = "00:07"
	wait(1)
	Text = "00:06"
	wait(1)
	Text = "00:05"
	wait(1)
	Text = "00:04"
	wait(1)
	Text = "00:03"
	wait(1)
	Text = "00:02"
	wait(1)
	Text = "00:01"
	wait(1)
	Text = "00:00"
end

CycleDayEvent.OnClientEvent:Connect(function()
	Timer()
end)

So far, I’ve tried making a script to cancel it which is as follows:

local ReplicatedStorage = game.ReplicatedStorage
local RemoteEvents = ReplicatedStorage.RemoteEvents
local CancelCycleTimerEvent = RemoteEvents.CancelCycleTimerEvent

local function CancelTimer()
	local PlayerGui = game.Players.LocalPlayer.PlayerGui
	local TextLabel = PlayerGui.Main.TextLabel
	local TextLabelClone = ReplicatedStorage.Extras.TextLabel:Clone()
	
	TextLabel:Destroy()
	TextLabelClone.Parent = PlayerGui.Main
	TextLabelClone.Name = "TextLabel" 
end

I feel like what I’ve scripted is wrong and I don’t know what about the script could be wrong. If anyone could look over the scripts and see how I can change the CancelEvent script much would be appreciated. Ask questions if needed.

Thanks!

1 Like

I think you can solve the problem by making an actual timer.
Don’t use wait as a timer it’s a bad idea.
Use loops, i don’t know how but i think that’s the better option.

1 Like

Just add a simple check!

local check = false

cancelEvent.OnClientEvent:Connect(function()
check = true;
end)

-- Timer loop
local timer = (15 * 60) -- 15 MINUTES
while wait(1) do
timer = timer -=1
local format = string.format("%i minutes %02i seconds", timer/ 60, timer % 60); 
TextLabel.Text =  format;
if check then
check = false
break -- ends it
end
if timer == 0 or timer < 1 then
timer = 15*60
continue -- if u want it to instant restart; otherwise use break
end
end
1 Like

Would this go into the CancelTimer script?

You can use that as your entire client timer script as it has both cancel, and the timer itself.

1 Like

Not what you wanted but chanage the timer function to this

local currentTime = 1200 --20 minutes
local text = ""
function timer()
	repeat
		wait(60)
		currentTime -= 60
		text = currentTime..":"
	until currentTime == 60

	repeat
		wait(1)
		currentTime -= 1
		text = "00:"..currentTime
	until currentTime = 0
end
1 Like