Value printing incorrectly whenever player gets a kill

I want to make a system that uses player knockouts to increase the round like in a boxing match.

The problem is that the first match prints the round values correctly by starting from zero and working its way up to 3 every time a player in the match gets a knockout, but every match after it prints random amounts starting from 1, 2 or 3 whenever a player gets a knockout.

I’ve tried changing the math and looking for other solutions, but had no luck.

I don’t think pictures will explain my problem easily, so I’ll show the code here:

local knockout = player.Store.KO
local round = 0
if Match == true then
    print("match is true")
	knockout.Changed:Connect(function(newVal) 
		round = newVal/newVal + 1 + round - 1
		print(round) --this is where it prints the round value each time the players kockout value changes
		if round == 3 then
			Countdown = 3
			round = 0
			Match = false
			print("Match has ended")
		end
	end)
end
1 Like

I am not understanding what are you trying to do in this formula, you’re going to have an issue if newVal is zero, because you’d be doing 0/0, which is infinity/math.huge. Wouldn’t it be better to just do

round = newVal

instead?

1 Like

Well I think it is the math. I don’t see the point why you need to divide a number by itself.

As you see, newVal/newVal = 1 or 0 if the round is zero. Then you add 1 then you add the round(0) minus 1. So that would mean that round 0+1+0-1 = 0 which is the reason.

1 Like

The newVal variable is connected to the players total knockouts, so it would just go past 3. I’m not too sure about the math I have to use to make it work.

Can’t you just add 1 to the round when knockout is changed?

local knockout = player.Store.KO
local round = 0
if Match == true then
    print("match is true")
	knockout.Changed:Connect(function(newVal) 
		round += 1
		print(round) --this is where it prints the round value each time the players kockout value changes
		if round == 3 then
			Countdown = 3
			round = 0
			Match = false
			print("Match has ended")
		end
	end)
end

I’ll try that in the script then.

It did work with the operator, but when I tried it in the second match it did the same thing:
https://i.gyazo.com/e5064ca61f003650003f85b9f106700b.mp4

I suggest putting the if statement inside the .Changed function as each time it’ll check if the match is true rather than only checking once when the script initially runs. That may help.

knockout.Changed:Connect(function(newVal) 
    if match == true then
— the rest of the script here
    end
end)
   

Ok I’ll try this out in the function.