Certain part of script runs multiple times without reason

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to increase the value of XP however it increases multiple times

  1. What is the issue? Include screenshots / videos if possible!
    image

However it increases multiple times as you can see via the printing.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Tried discord + youtube + devhub

Local script:

	endoflevel.Touched:Connect(function(hit)
				if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and db == false then
					db = true
					print("IF MULTIPLE LEAVE")
					hrp.CFrame = game.Workspace.SpawnLocation.CFrame * CFrame.new(0, 5, 0)

					if levelstarted ~= nil then

						local timer = os.clock() - levelstarted

						timer = timer * 1000
						timer = math.round(timer)
						local other = timer
						timer = timer/1000

						timetxt.Text = "Time: "..timer
						levelstarted = nil

						if tonumber(txt.Text) == 0.0000 then
							txt.Text = tostring(timer)
							event:FireServer(timer, key, num)

							task.wait(2)
							--valincrease:FireServer(plr.leaderstats.Level.Value, XP, Coins, other)

							valincrease.OnClientEvent:Connect(function(NewCoins, NewXP)
								coinstxt.Text = "Coins gained: ".. NewCoins
								xptxt.Text = "XP gained: ".. NewXP

								rewards.Visible = true
							end)
							task.wait(2)
							db = false

						elseif tonumber(txt.Text) > timer then
							txt.Text = tostring(timer)
							event:FireServer(timer, key, num)

							task.wait(2)
							--	valincrease:FireServer(plr.leaderstats.Level.Value, XP, Coins, other)

							valincrease.OnClientEvent:Connect(function(NewCoins, NewXP)
								coinstxt.Text = "Coins gained: ".. NewCoins
								xptxt.Text = "XP gained: ".. NewXP

								rewards.Visible = true
							end)
							db = false
						else
							print("Value increase")
							valincrease:FireServer(plr.leaderstats.Level.Value, XP, Coins, other)
							valincrease.OnClientEvent:Connect(function(NewCoins, NewXP)
								coinstxt.Text = "Coins gained: ".. NewCoins
								xptxt.Text = "XP gained: ".. NewXP

								rewards.Visible = true
							end)

							task.wait(2)
						end
						task.wait(5)
						db = false
					end
				end
			end)

Server Script:

valincrease.OnServerEvent:Connect(function(plr, Level, XP, Coins, other, num)
	if db1 == false then
		print("fird")
		db1 = true

		for i, v in ipairs(PlayerTimes) do
			if i == num then
				if v > other then
					print("1)"..XP)
					local multipler = Level/4
					multipler = math.floor(multipler)
					XP *= multipler
					print("2)"..XP)
					Coins *= multipler
					math.round(XP)
					print("3"..XP)
					math.round(Coins)
					plr.leaderstats.XP.Value += XP
					plr.leaderstats.Coins.Value += Coins
					valincrease:FireClient(plr, Coins, XP)
				end
			else
				print("1)"..XP)
				local multipler = 1
				XP *= multipler
				print("2)"..XP)
				Coins *= multipler
				math.round(XP)
				print("3)"..XP)
				math.round(Coins)
				plr.leaderstats.XP.Value += XP
				plr.leaderstats.Coins.Value += Coins
				valincrease:FireClient(plr, Coins, XP)
			end
		end
		task.wait(3)
		db1 = false
	end
end)

It has nothing to do with the local script (i believe) firing the event multiple times because i already used printing to debug it however the server script seems to run multiple times though.

Thanks for the help!

If you want it to only run once then why don’t you break the loop? By what I am seeing you are using a for loop and that is why it is printing multiple times. Why do you need to use a for loop? (I want to know why to see where we can break the loop)

I’m using a for loop to find the best time for that level and check if the plyer beat it or not.

Why do you not want the script to run multiple times then? Since you have an “else” every iteration will call a remote event/print.

1 Like

Ah I see I misplaced the “else” i’ll sort that now

Yep fixed it now. Thanks!

valincrease.OnServerEvent:Connect(function(plr, Level, XP, Coins, other, num)
	if db1 == false then
		print("fird")
		db1 = true

		for i, v in ipairs(PlayerTimes) do
			if i == num then
				if v > other then
					print("1)"..XP)
					local multipler = Level/4
					multipler = math.floor(multipler)
					XP *= multipler
					print("2)"..XP)
					Coins *= multipler
					math.round(XP)
					print("3"..XP)
					math.round(Coins)
					plr.leaderstats.XP.Value += XP
					plr.leaderstats.Coins.Value += Coins
					valincrease:FireClient(plr, Coins, XP)
				else
					print("1)"..XP)
					local multipler = 1
					XP *= multipler
					print("2)"..XP)
					Coins *= multipler
					math.round(XP)
					print("3)"..XP)
					math.round(Coins)
					plr.leaderstats.XP.Value += XP
					plr.leaderstats.Coins.Value += Coins
					valincrease:FireClient(plr, Coins, XP)
				end
			end
		end
		task.wait(3)
		db1 = false
	end
end)

1 Like

One more thing, break the loop just in case:

valincrease.OnServerEvent:Connect(function(plr, Level, XP, Coins, other, num)
	if db1 == false then
		print("fird")
		db1 = true

		for i, v in ipairs(PlayerTimes) do
			if i == num then
				if v > other then
					print("1)"..XP)
					local multipler = Level/4
					multipler = math.floor(multipler)
					XP *= multipler
					print("2)"..XP)
					Coins *= multipler
					math.round(XP)
					print("3"..XP)
					math.round(Coins)
					plr.leaderstats.XP.Value += XP
					plr.leaderstats.Coins.Value += Coins
					valincrease:FireClient(plr, Coins, XP)
				else
					print("1)"..XP)
					local multipler = 1
					XP *= multipler
					print("2)"..XP)
					Coins *= multipler
					math.round(XP)
					print("3)"..XP)
					math.round(Coins)
					plr.leaderstats.XP.Value += XP
					plr.leaderstats.Coins.Value += Coins
					valincrease:FireClient(plr, Coins, XP)
				end
				break --Here we ensure that it won't run twice.
			end
		end
		task.wait(3)
		db1 = false
	end
end)
1 Like