Help debugging a simple script

Hey there I’m wisematheu.

I’ve got a simple script which counts how many times the user clicks a TextButton however every time the loop repeats, the number goes up by one. For example the first time, it adds 1, the second time 2 and the third time 3. I’m not sure why this is happening and I’ve reviewed my code numerous times and still can’t find the issue.

The first time it goes up by one
image

The third time it goes up by three
image

Code

Explorer
image

local player = game.Players.LocalPlayer

storage = game.Workspace.Player4.Leaderboard.Storage
timeRemainingGui = script.Parent.Parent.TimeRemaining

while true do
	lengthOfTime = script.Parent.Parent.TextBox
	script.Parent.Parent.TextBoxHeader.Text = "Length: "..lengthOfTime.Text.." (click to customize)"
	timeRemaining = 0
	if tonumber(lengthOfTime.Text) < 1 then
		timeRemaining = 10
		lengthOfTime.Text = "Please enter a number that's 1 or above"
		warn("Number is lower than 1, time automatically set to 10")
	else
		timeRemaining = lengthOfTime.Text
		totalTime = lengthOfTime.Text
	end
	
	CPxS = 0

	wait(5)
	running = true
	script.Parent.Text = "CLICK HERE"
	timeRemainingGui.Text = timeRemaining
	script.Parent.Parent.Clicks.Text = "Clicks: "

	script.Parent.MouseButton1Down:Connect(function()
		if running == true then
			CPxS += 1
			script.Parent.Parent.Clicks.Text = "Clicks: "..CPxS
		end
	end)
	
	while tonumber(timeRemaining) >= 0 do
		wait(1)
		timeRemainingGui.Text -= 1
		timeRemaining -= 1
	end
	
	timeRemainingGui.Text = "INTERMISSION"
	running = false
	
	CPS = 0
	
	CPS = CPxS/totalTime
	
	script.Parent.Text = "END"
	print("Score = "..CPS)
	wait(1)
	
	if CPS > 0 then
		result = storage.Result:Clone()
		result.Parent = game.Workspace.Player4.Leaderboard.SurfaceGui.Frame.Frame
		result.Text = "   "..CPS
		result.Name = CPS
		result.LayoutOrder = -CPS
	end
	
	wait(1)
	script.Parent.Text = "WAIT"
	script.Parent.Parent.Clicks.Text = "Clicks: "
end

Updated code

local player = game.Players.LocalPlayer

storage = game.Workspace.Player4.Leaderboard.Storage
timeRemainingGui = script.Parent.Parent.TimeRemaining

while true do
	lengthOfTime = script.Parent.Parent.TextBox
	script.Parent.Parent.TextBoxHeader.Text = "Length: "..lengthOfTime.Text.." (click to customize)"
	timeRemaining = 0
	if tonumber(lengthOfTime.Text) < 1 then
		timeRemaining = 10
		lengthOfTime.Text = "Please enter a number that's 1 or above"
		warn("Number is lower than 1, time automatically set to 10")
	else
		timeRemaining = lengthOfTime.Text
		totalTime = lengthOfTime.Text
	end
	
	CPxS = 0

	wait(5)
	running = true
	script.Parent.Text = "CLICK HERE"
	timeRemainingGui.Text = timeRemaining
	script.Parent.Parent.Clicks.Text = "Clicks: "
	
	CPxS = 0
	script.Parent.MouseButton1Down:Connect(function()
		if running == true then
			CPxS += 2
			script.Parent.Parent.Clicks.Text = "Clicks: "..CPxS
		end
	end)
	
	while tonumber(timeRemaining) >= 0 do
		wait(1)
		timeRemainingGui.Text -= 1
		timeRemaining -= 1
	end
	
	timeRemainingGui.Text = "INTERMISSION"
	running = false
	
	CPS = 0
	
	CPS = CPxS/totalTime
	
	script.Parent.Text = "END"
	print("Score = "..CPS)
	wait(1)
	
	if CPS > 0 then
		result = storage.Result:Clone()
		result.Parent = game.Workspace.Player4.Leaderboard.SurfaceGui.Frame.Frame
		result.Text = "   "..CPS
		result.Name = CPS
		result.LayoutOrder = -CPS
	end
	
	wait(1)
	script.Parent.Text = "WAIT"
	script.Parent.Parent.Clicks.Text = "Clicks: "
end

remove the click connection outside the while loop, there are multiple connections happening. Yep like @BubasGaming is saying. This article should help with events Handling Events.

local player = game.Players.LocalPlayer

storage = game.Workspace.Player4.Leaderboard.Storage
timeRemainingGui = script.Parent.Parent.TimeRemaining

	script.Parent.MouseButton1Down:Connect(function()
		if running == true then
			CPxS += 2
			script.Parent.Parent.Clicks.Text = "Clicks: "..CPxS
		end
	end)


while true do
	lengthOfTime = script.Parent.Parent.TextBox
	script.Parent.Parent.TextBoxHeader.Text = "Length: "..lengthOfTime.Text.." (click to customize)"
	timeRemaining = 0
	if tonumber(lengthOfTime.Text) < 1 then
		timeRemaining = 10
		lengthOfTime.Text = "Please enter a number that's 1 or above"
		warn("Number is lower than 1, time automatically set to 10")
	else
		timeRemaining = lengthOfTime.Text
		totalTime = lengthOfTime.Text
	end
	
	CPxS = 0

	wait(5)
	running = true
	script.Parent.Text = "CLICK HERE"
	timeRemainingGui.Text = timeRemaining
	script.Parent.Parent.Clicks.Text = "Clicks: "
	
	CPxS = 0
	
	while tonumber(timeRemaining) >= 0 do
		wait(1)
		timeRemainingGui.Text -= 1
		timeRemaining -= 1
	end
	
	timeRemainingGui.Text = "INTERMISSION"
	running = false
	
	CPS = 0
	
	CPS = CPxS/totalTime
	
	script.Parent.Text = "END"
	print("Score = "..CPS)
	wait(1)
	
	if CPS > 0 then
		result = storage.Result:Clone()
		result.Parent = game.Workspace.Player4.Leaderboard.SurfaceGui.Frame.Frame
		result.Text = "   "..CPS
		result.Name = CPS
		result.LayoutOrder = -CPS
	end
	
	wait(1)
	script.Parent.Text = "WAIT"
	script.Parent.Parent.Clicks.Text = "Clicks: "
end

I think i know why. your mousebutton1click event is in while loop but it shouldn’t.

1 Like

oh wait, nvm i am wrong, i didn’t look at updated version

Does your script work now or still not?

Yep, thanks both of you! :slight_smile: I’ll give @dthecoolest the solution as he replied first though