While wait loop returning false info

  1. What do you want to achieve? Keep it simple and clear!
    When creating a while wait() do loop followed by else and if statements, for some reason the functions passed through are not correct or atleast before you reset.

  2. What is the issue? Include screenshots / videos if possible!
    For some reason, the loop is working completely fine, but passing through the wrong information before resetting.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Having found anything, perhaps there is an easy solution.

In my code, there is continuous loop that loops through the player’s leaderstats to update it. The code works by placing different amounts of Trophies in specific groups and updating what group they’re in through the amount of Trophies, they have.

This is what a simplified version of my code looks like

while wait() do
	
	if player.leaderstats.Trophies.Value == 100  or player.leaderstats.Trophies.Value < 249 then
				print("Beginner reached")
        else
	
    if player.leaderstats.Trophies.Value == 250  or player.leaderstats.Trophies.Value < 499 then					
				print("Bronze reached")

What happens is that the loop works fine and prints, however it’s getting the the wrong number of Trophies in leaderstats. For example, if have 300 Trophies, it prints out the first statement and assumes it should call the functions that follow it. The only time the leaderstats value matches with the one of the if statements is when you reset, or in my case a round resets the player. As a result the loop resets as well, but with the right information upon respawn. Is there any reason this may be, I’m quite positive that I’m missing something in the code.

I was just facing this problem with my terrain generation script. The problem is that if a value is for example 500. It will be both greater than 499 and 249, making it fire both. The way I found to fix this is to treat it as if you were doing AABB collision by giving a maximum for all except for the highest minimum.

ah, seems to make sense. But why does it fire both if the values are specifically different?

Can you print what “trophies” is when this error happens?

You mean the value of the amount of trophies? Did it, and it prints 0? This is quite strange because my leaderstats amount is not equal to 0.

Maybe you’re not getting player properly causing it to return nil? I also remember from a script I made a while ago I seemed to not be able to access data set on the server from the client.

Hmm. It seems to be working fine when the player resets, but not when joining. It’s prints out the first statement, yet the other else statements are working. I definitely know my leaderstats is not equal to 0 :thinking:

Ok it seems to be somewhat working. What I mean by this is the first print get printed in the loop but after some time the prints change, and it updates the leaderstats. This is a problem because any function called by the first statement is still present and doesn’t change. Logically, it should print that out if my leaderstats is not equal to it.

Sorry if my interpretation of this is wrong, but it seems to me that you want it to print “Beginner reached” when their trophies are between 100 and 249, and “Bronze reached” between 250 and 499, so what I believe you need to do is:

while wait() do
    if player.leaderstats.Trophies.Value > 99  and player.leaderstats.Trophies.Value < 250 then
				print("Beginner reached")
    elseif player.leaderstats.Trophies.Value > 249  or player.leaderstats.Trophies.Value < 500 then					
				print("Bronze reached")
    end
end

I am not sure if this will actually solve it, but if nothing is nil, then I think this might do something.

Ok, I tried this way but it has gotten worse. The functions carried by the else statements are wrong. The print for leaderstats amount is working though. It’s hard and messy to implement “elseif” for each trophy group considering I have quite a bit of things to pass through, e.g what the text says for each group, etc.

Is there any to make it more easier to do this?

In that case, maybe you want to try taking a step back and approach this in a different direction? From what I understood from your reply, you have a bunch of nested if statements that make it difficult to find the problem, so my suggestion to you is that you break down your code into functions, or breaking it down into a module would be better.

Ideally, you want to have as few if statements as you can.

Consider using .Changed or :GetPropertyChangedSignal() rather than a while wait() do loop, I’m sure these values do not need to be checked every 1/30th of a second.

Ok, i’ll try that, even when it updates 1/30th of a second, it still errors.

Well I think my code is fine, and is it really necessary to use module? I’m simply passing through the player leaderstats amount and updating it through a gui. Even if I was to use a module, I’m more asking on how to simply rearrange if and else statments in a code so it’s not repetitive and more efficient.

I mean, you did ask how to make it easier, and modules make things easy and efficient. Another way we can help you, is if you could include what else you are checking and where you are checking it? We could help you restructure it to make it easier to read.

Ok, I’ll give an insight into what I’m checking. This is my code and it’s all done via local script. I know it’s quite messy and I need to refactor some things.

local player = game.Players.LocalPlayer
local Frame = script.Parent.Parent.Parent:WaitForChild("Frame")
local TrophyFrame = script.Parent.Parent:WaitForChild("Trophies")
local TrophyIndicator = script.Parent:WaitForChild("TrophyIndicator")
local TrophyLabel = script.Parent.Parent:WaitForChild("TrophyLabel")

-- Trophies
local TrophiesLeft1 = script.Parent:WaitForChild("TrophiesLeft1")
local TrophiesLeft2 = script.Parent:WaitForChild("TrophiesLeft2")
local TrophiesLeft3 = script.Parent:WaitForChild("TrophiesLeft3")
local TrophiesLeft4 = script.Parent:WaitForChild("TrophiesLeft4")
local TrophiesLeft5 = script.Parent:WaitForChild("TrophiesLeft5")
local TrophiesLeft6 = script.Parent:WaitForChild("TrophiesLeft6")
local TrophiesLeft7 = script.Parent:WaitForChild("TrophiesLeft7")

local Beginner = script.Parent:WaitForChild("BeginnerLeague")
local Bronze = script.Parent:WaitForChild("BronzeLeague")
local Silver = script.Parent:WaitForChild("SilverLeague")
local Gold = script.Parent:WaitForChild("GoldLeague")
local Adamant = script.Parent:WaitForChild("AdamantLeague")
local Ruby = script.Parent:WaitForChild("RubyLeague")
local Diamond = script.Parent:WaitForChild("DiamondLeague")
local Mythical = script.Parent:WaitForChild("MythicalLeague")

while wait() do
	
	if player.leaderstats.Trophies.Value == 100  or player.leaderstats.Trophies.Value < 249 then
				print("Beginner reached")
				TrophyIndicator.Position = Beginner.Position - UDim2.new(0,0,-0.35,0)
				TrophyIndicator.AnchorPoint = Vector2.new(0,0,0.5,0)
				TrophyIndicator.Visible = true
				TrophiesLeft1.Visible = true
				TrophyLabel.Text = "Congratulations, you are in Beginner League!"
				TrophiesLeft1.Text = 250 - player.leaderstats.Trophies.Value.. "Trophies Left"	 
		
		else
			if player.leaderstats.Trophies.Value == 250  or player.leaderstats.Trophies.Value < 499 then					
				print("Bronze reached")
				TrophyIndicator.Position = Bronze.Position - UDim2.new(0,0,-0.35,0)
				TrophyIndicator.AnchorPoint = Vector2.new(0,0,0.5,0)
			   	TrophyIndicator.Visible = true
				TrophiesLeft2.Visible = true
				TrophyLabel.Text = "Congratulations, you are in Bronze League!"
				TrophiesLeft2.Text = 500 - player.leaderstats.Trophies.Value.. "Trophies Left"	 
			
		else
			if player.leaderstats.Trophies.Value == 500  or player.leaderstats.Trophies.Value < 999 then
				print("Silver reached")
				TrophyIndicator.Position = Silver.Position - UDim2.new(0,0,-0.35,0)
				TrophyIndicator.AnchorPoint = Vector2.new(0,0,0.5,0)
				TrophyIndicator.Visible = true
				TrophiesLeft3.Visible = true
				TrophyLabel.Text = "Congratulations, you are in Silver League!"
				TrophiesLeft3.Text = 1000 - player.leaderstats.Trophies.Value.. "Trophies Left"	 
		else
			if player.leaderstats.Trophies.Value == 1000  or player.leaderstats.Trophies.Value < 1499 then
				print("Gold reached")
				TrophyIndicator.Position = Gold.Position - UDim2.new(0,0,-0.35,0)
				TrophyIndicator.AnchorPoint = Vector2.new(0,0,0.5,0)
				TrophyIndicator.Visible = true	
				TrophiesLeft4.Visible = true
				TrophyLabel.Text = "Congratulations, you are in Gold League!"
				TrophiesLeft4.Text = 1500 - player.leaderstats.Trophies.Value.. "Trophies Left"	 
					
		else
			if player.leaderstats.Trophies.Value == 1500  or player.leaderstats.Trophies.Value < 1999 then
				print("Admant reached")
				TrophyIndicator.Position = Adamant.Position - UDim2.new(0,0,-0.35,0)
				TrophyIndicator.AnchorPoint = Vector2.new(0,0,0.5,0)
				TrophyIndicator.Visible = true
				TrophiesLeft5.Visible = true
				TrophyLabel.Text = "Congratulations, you are in Adamant League!"
				TrophiesLeft5.Text = 2000 - player.leaderstats.Trophies.Value.. "Trophies Left"	
						
		else		
			if player.leaderstats.Trophies.Value == 2000  or player.leaderstats.Trophies.Value < 2499 then
				print("Ruby reached")
				TrophyIndicator.Position = Ruby.Position - UDim2.new(0,0,-0.35,0)
				TrophyIndicator.AnchorPoint = Vector2.new(0,0,0.5,0)
				TrophyIndicator.Visible = true
				TrophiesLeft6.Visible = true		
				TrophyLabel.Text = "Congratulations, you are in Ruby League!"
				TrophiesLeft6.Text = 2500 - player.leaderstats.Trophies.Value.. "Trophies Left"					
							
		else		
			if player.leaderstats.Trophies.Value == 2500 or player.leaderstats.Trophies.Value < 2999 then
				print("Diamond reached")
				TrophyIndicator.Position = Diamond.Position - UDim2.new(0,0,-0.35,0)
				TrophyIndicator.AnchorPoint = Vector2.new(0,0,0.5,0)
				TrophyIndicator.Visible = true
				TrophiesLeft7.Visible = true
				TrophyLabel.Text = "Congratulations, you are in Diamond League!"
				TrophiesLeft7.Text = 3000 - player.leaderstats.Trophies.Value.. "Trophies Left"	
								
		else			
			if player.leaderstats.Trophies.Value >= 3000 then
				print("Mythical reached")
				TrophyIndicator.Position = Mythical.Position - UDim2.new(0,0,-0.35,0)
				TrophyIndicator.AnchorPoint = Vector2.new(0,0,0.5,0)
				TrophyIndicator.Visible = true	
				TrophyLabel.Text = "Congratulations, you are in Mythical League!"
										
									
								end
							end
						end
					end	
			
				end
				
			end
		end
	end
end

You really should use elseif instead of else if then you won’t end up with this nested mess.

As someone else pointed out there is also a logical flaw, all of you == should be >=.

That aside, it sounds like you might be running into trouble of either reading the value too soon before it has been updated with the right value, or maybe you’re changing the value on the client and reading it on the server, or something along those lines.

Change this from a loop to a .Changed event to ensure you don’t prematurely run this, and to avoid printing the same thing again and again. This way you can identify the exact moment that the value changes and that might help to debug the issue as to why it isn’t happening when you expect it to.

Right now there are too many superficial issues to solve the underlying issue.

2 Likes

That is all extremely repetitive, so I will turn it into a module script for you to show you how simple this all could be, just give me a few minutes.

1 Like

Thanks for this! I guess I wasn’t clear about my absolute disorganised coding. One question, though,

How is this

else 
  if

any different to

elseif