Timer gets added random values when event is fired

I have this timer and when you eat vitamins it gives you 45 seconds so I made a bindable event to communicate between 2 clients and I did “currenttime += 10”. When I played it in game it would give me random numbers and add 2 minutes or 6 seconds. How do I get it just to give 10 seconds? All help would be greatly appreciated!

1 Like

Could you please provide the script?

2 Likes

The script is very long but I can provide the event scripts and if that doesn’t I will try sending the timer script

1 Like
local function startTimer()
	while true do
		currentTime -= 1
		label.Text = formatTime()
		
		Addtime.Event:Connect(function()
			currentTime += 2
		end)
		
		if currentTime <= 30 then
			label.TextColor3 = Color3.new(1, 0, 0.0156863)
		end
		
		if currentTime <= 10 then
			game:GetService("SoundService").Beep:Play()
		end
		
		if currentTime <= 0 then
			char.Humanoid.Health = 0
			game:GetService("SoundService").Beep:Stop()
			label.Parent:Destroy()
			label:Destroy()
			break
			
		end

		task.wait(1)
	end
end

startTimer()
2 Likes

The script above this script is just formatting for the timer btw

1 Like

Can I see the actual part of the script were you add time and send the event please?

1 Like

its in the middle and its called "Addtime.Event:Connect(function())

1 Like
local currentTime = 480

is the variable I tried adding too

1 Like

Oh sorry I mean the other end of the bindable event.

1 Like

Its nothing the bindable event just says fire when its met under a certain condition so Addtimer:Fire() was all!

1 Like

May I ask, what is that certain condition?

2 Likes

a function lol

script.Parent.Activated:Connect(function()
	AnimEvent:FireServer()
	Addtime:Fire()
	SoundService.VitaminSFX.Eat:Play()
	Vitamins:Destroy()
end)
1 Like

Make sure the place you bind this function isn’t in a loop or another event that can be called multiple times, otherwise it’s reasonable that the behavior is this way. Of course, I can’t really tell where this is by the amount of code you gave, but it’s a mistake I do a lot.

1 Like

Not in a loop. That is the only neccassary code and it randomly multiplies sometimes by 5 and gives out really random stuff.

1 Like

Once I ate the vitamins one time it gave me 10 seconds and the other time 20 seconds.

This time it gave me 15 seconds!!

I guess it is looping, I will add a debounce and see how it works since I printed out how many times it was adding and it gave me a long list.

If this is not resolved, basically what happens is that you connect an event inside a loop, this means that the connection is created over and over again and when it fires, currentTime will increase X amount of times (how many times the loop is reactivated * 2)

The solution is to move the connection and disconnect it when the loop ends

local function startTimer()
	local Event = Addtime.Event:Connect(function()
		currentTime += 2
	end)
	while true do
		currentTime -= 1
		label.Text = formatTime()


		if currentTime <= 30 then
			label.TextColor3 = Color3.new(1, 0, 0.0156863)
		end

		if currentTime <= 10 then
			game:GetService("SoundService").Beep:Play()
		end

		if currentTime <= 0 then
			char.Humanoid.Health = 0
			game:GetService("SoundService").Beep:Stop()
			label.Parent:Destroy()
			label:Destroy()
			break

		end

		task.wait(1)
	end
	Event:Disconnect()
end

startTimer()
1 Like

That is what it has been doing! I just figured that out myself when I saw the code

Thanks alot! I learned alot from this