Help with my code

Hello, I’ve been really confused lately about this piece of code, essentially what I’m trying to do is make it so a notification appears when a stat in the game is going down to a certain number, and that works problem is when the stat goes up it also runs and this code should fix it but it doesn’t seem to work. Any help or suggestions would be amazing thanks!
Code:

old_Val = nil

if old_Val == nil then
	old_Val = 100
	print(old_Val)
end
function Stats:WarnIfLow(statName, statValue)

	print(old_Val)
	print("StatVal: "..statValue) 
	if (statValue == 70 or statValue == 60 or statValue == 50) and Instances.Stats.Oxygen.Value == 0 and old_Val < statValue then
		Instances.Survivor:Notify(statName.." is at "..statValue.."!", "Warning")
	end
	if old_Val <= 70 then
		old_Val = 71
	end
	old_Value = statValue
end

Essentially I just need this to run when the value is going down to 70,60 or 50.

4 Likes

Maybe try this one?

old_Val = nil

if old_Val == nil then
	old_Val = 100
	print(old_Val)
end
function Stats:WarnIfLow(statName, statValue)

	print(old_Val)
	print("StatVal: "..statValue) 
	if (statValue == 70 or statValue == 60 or statValue == 50) and Instances.Stats.Oxygen.Value == 0 and old_Val < statValue then

        if old_Val = statValue + 1 then return end
		Instances.Survivor:Notify(statName.." is at "..statValue.."!", "Warning")
	end
	if old_Val <= 70 then
		old_Val = 71
	end
	old_Value = statValue
end
2 Likes

Will do thx, I’ll tell you if it works.

1 Like

It changes the value but the notification won’t show up.

1 Like

Try this one,

if old_Val == nil then
	old_Val = 100
	print(old_Val)
end
function Stats:WarnIfLow(statName, statValue)

	print(old_Val)
	print("StatVal: "..statValue) 
	if (statValue == 70 or statValue == 60 or statValue == 50) then 
      if Instances.Stats.Oxygen.Value == 0 and old_Val < statValue then
		Instances.Survivor:Notify(statName.." is at "..statValue.."!", "Warning")
	end
end
	if old_Val <= 70 then
		old_Val = 71
	end
	old_Value = statValue
end

If this one don’t work idk what to do lol.

2 Likes

I will try my best to help

try print what the value at the end to see what you get first

	if old_Val <= 70 then
		old_Val = 71
	end
    print(statValue)
    print(old_Val)
	old_Value = statValue

Could you show your notification script? So we would know if the notification script is the problem.

1 Like

It’s complicated, but I know it’s not the function nor the notification, since it works and it’s obfusticated I believe so I can’t do much about it.

Alright, I’ll try this one, at first this job was looked really easy but it’s just weird Idk what’s really wrong with it.

Okay, so I did some testing and I found out this is where the code stops:

So I’m kinda unsure why it stops there when this screenshot shows that the old value is bigger than the stat value. (Stat Value is called StatVal: [Num] and the old Value is just [Num])
image
Any theories or reasons why this may be happening?

Wait… I just realized… that I put the less than sign instead of greater than… I’m legit braindead hold on lemme see if that’s it.

And of course, it is it’s that…

Nvm I mean the notification works but it still shows once you regain it.