Local Script Listener Doesn't Work

Hello.

I have created a script which listens to a remote event, but when the remote event gets fired, nothing happens (but, another local script does work from the same event). The script is as follows:

CountdownEvent = game.ReplicatedStorage.CountdownEvent
Countdown = script.Parent.Countdown
CountDownFinishEvent = game.ReplicatedStorage.CountdownFinish

local Warning = script.Parent.WARNING

local WarningInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)

local Warning1 = game:GetService("TweenService"):Create(Warning, WarningInfo, {TextTransparency = 0})
local Warning2 = game:GetService("TweenService"):Create(Warning, WarningInfo, {TextTransparency = 0.75})

local WarningEnd = game:GetService("TweenService"):Create(Warning, WarningInfo, {TextTransparency = 1})

local Done = false

function WarningBegin()
	while true do
		print("Looped!")
		CountDownFinishEvent.OnClientEvent:Connect(function()
			WarningEnd:Play()
			print("Warning Breaked")
			Done = true
			return 
		end)
		if Done == true then
			break
		end
		Warning1:Play()
		wait(1)
		CountDownFinishEvent.OnClientEvent:Connect(function()
			WarningEnd:Play()
			print("Warning Breaked")
			Done = true
			return 
		end)
		if Done == true then
			break
		end
		Warning2:Play()
		wait(1)
	end
end

CountdownEvent.OnClientEvent:Connect(WarningBegin)

Thanks for the help!

EDIT - The remote event is the CountdownEvent, not CountDownFinishEvent.

No clue about the event listener, its implementation locally seems fine. Check your output, maybe provide server code here where the event is fired on server because thats relevant.

But dude, you got a memory leak problem here. In the loop, you’re connecting a listener every second. After a minute, you’ll have ‘Warning Breaked’ printed 60 times, because you never disconnect them.

You want something like:

function WarningBegin()
	local Done = false
	local waitOnFinish = CountDownFinishEvent.OnClientEvent:Connect(function()
		WarningEnd:Play()
		print("Warning Breaked")
		Done = true
		return 
	end)

	while true do
		print("Looped!")
		if Done then
			break
		end
		Warning1:Play()

		wait(1)

		if Done then
			break
		end
		Warning2:Play()
		wait(1)
	end

	waitOnFinish:Disconnect()
end

CountdownEvent.OnClientEvent:Connect(WarningBegin)
1 Like

Thanks for the more efficient code! There is nothing in the output, though, which is extremely weird.

Don’t know how, or why, but when I put a () in front of the WarningBegin it worked. Thanks for the help anyways!