Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is my for i loop going straight to "else"?

So I have a for i, v statement followed by an if statement and then an else. For some reason, upon clicking the GUI, it goes straight to the else statement first and then goes to the regular if statement. I know this because the GUI is supposed to have its text changed when it is else.

itembutton.MouseButton1Click:connect(function()
	for i, v in pairs(game.ReplicatedStorage:GetChildren()) do
	if v.Name == player.Name and v.Coins.Value >=100 and (not player.Backpack:FindFirstChild('Potion') or player.Character:FindFirstChild('Potion')) then
	v.Coins.Value = v.Coins.Value - amount				
	game.Workspace.RemoteServer.RemoteEvent:FireServer()
	itembutton.Text = 'Purchased'
	itembutton.TextColor3 = BrickColor.Yellow().Color
	wait(0.1)
	itembutton.TextColor3 = BrickColor.White().Color
	wait(0.1)
	itembutton.TextColor3 = BrickColor.Yellow().Color
	wait(0.1)
	itembutton.TextColor3 = BrickColor.White().Color
	else
	itembutton.Text = 'Failed to purchase'
	itembutton.TextColor3 = BrickColor.Red().Color
	wait(0.1)
	itembutton.TextColor3 = BrickColor.White().Color
	wait(0.1)
	itembutton.TextColor3 = BrickColor.Red().Color
	wait(0.1)
	itembutton.TextColor3 = BrickColor.White().Color
	wait(0.1)
	itembutton.Text = indextext
	wait(0.5)
	end
	end
end)


5 comments

0

Because the if statement is false. (If one of the children's name is the players name, and the children's value is greater than or equal to 100, and not in the backpack, or in the character, then..blah.)

0

Why are you looping through all elements of the rep storage??

0

(not player.Backpack:FindFirstChild('Potion') or player.Character:FindFirstChild('Potion')) should have the not outside of the parentheses.

0

@NathanTheDeveloper You didn't read OP correctly. I stated that it FIRST goes to the else statement and THEN it goes to if it is true. Why is it doing this? I previously did not have the for i, v thing and I never had this issue, I know the statement is not false. The player has nothing in his backpack or Character and has more than enough coins but for some reason it puts "Failed to Purchase" abo

0

@ kingdom5 Hi, I am looking through all elements of Rep. storage because there is where I am keepving IntValues for the player's for DataStore. I was previously keeping them inside the Player but values were not being saved to the DataStore because it is set to save onPlayerRemoving and it just was not working, so I have resorted to this instead.

1 answer

✓ Accepted answer

First, tab your code correctly.

itembutton.MouseButton1Click:connect(function()
	for i, v in pairs(game.ReplicatedStorage:GetChildren()) do
		if v.Name == player.Name and v.Coins.Value >=100 and (not player.Backpack:FindFirstChild('Potion') or player.Character:FindFirstChild('Potion')) then
			v.Coins.Value = v.Coins.Value - amount              
			game.Workspace.RemoteServer.RemoteEvent:FireServer()
			itembutton.Text = 'Purchased'
			itembutton.TextColor3 = BrickColor.Yellow().Color
			wait(0.1)
			itembutton.TextColor3 = BrickColor.White().Color
			wait(0.1)
			itembutton.TextColor3 = BrickColor.Yellow().Color
			wait(0.1)
			itembutton.TextColor3 = BrickColor.White().Color
		else
			itembutton.Text = 'Failed to purchase'
			itembutton.TextColor3 = BrickColor.Red().Color
			wait(0.1)
			itembutton.TextColor3 = BrickColor.White().Color
			wait(0.1)
			itembutton.TextColor3 = BrickColor.Red().Color
			wait(0.1)
			itembutton.TextColor3 = BrickColor.White().Color
			wait(0.1)
			itembutton.Text = indextext
			wait(0.5)
		end
	end
end)

The problem

Here is a smaller piece of code that has the same problem that your code does.

local numbers = {1, 3, 5, 6, 7}

local foundEven = false

for _, value in pairs(numbers) do
	if value % 2 == 0 then
		foundEven = true
		-- the value is even!
		print("found even", value)
	else
		foundEven = false
		-- not even!
		print("found odd", value)
	end
end

print("final foundEven", foundEven)

If we run this code, we get this output:

found odd	1
found odd	3
found odd	5
found even	6
found odd	7
final foundEven	false

A for loop does something for each. That means the rejection of 7 causes foundEven for forget that you've ever found an odd.

You don't know that you've failed until after all of the things have been checked.

A better solution

You don't need a for loop at all here. Use :FindFirstChild.

itembutton.MouseButton1Click:connect(function()
	local data = game.ReplicatedStorage.PlayerData:FindFirstChild(player.Name)
	assert(data, "PlayerData not created yet for `" .. player.Name .. "`")
	
	if data.Coins.Value >=100 and (not player.Backpack:FindFirstChild('Potion') or player.Character:FindFirstChild('Potion')) then
		data.Coins.Value = data.Coins.Value - amount
		game.Workspace.RemoteServer.RemoteEvent:FireServer()
		itembutton.Text = 'Purchased'
		itembutton.TextColor3 = BrickColor.Yellow().Color
		wait(0.1)
		itembutton.TextColor3 = BrickColor.White().Color
		wait(0.1)
		itembutton.TextColor3 = BrickColor.Yellow().Color
		wait(0.1)
		itembutton.TextColor3 = BrickColor.White().Color
	else
		itembutton.Text = 'Failed to purchase'
		itembutton.TextColor3 = BrickColor.Red().Color
		wait(0.1)
		itembutton.TextColor3 = BrickColor.White().Color
		wait(0.1)
		itembutton.TextColor3 = BrickColor.Red().Color
		wait(0.1)
		itembutton.TextColor3 = BrickColor.White().Color
		wait(0.1)
		itembutton.Text = indextext
		wait(0.5)
	end
end)

FilteringEnabled

You shouldn't be using the client to change the values. This should be done by the server. Likely the money should be subtracted (and double checked that you have enough) in the RemoteEvent.

This discussion is preserved as it was. New answers, comments, and votes are closed.