If statement not working with variable

Hi everyone, new scripter here with a question. This if statement seems to always jump to “else” even if the conditions I stated at the top are correct (I checked the values in Explorer)

storage.Lanes.PassScanner.OnServerEvent:Connect(function(player, lane)
	local char = script.Parent
	if char.QueuePosition.Value == 0 and char.LaneAssignment.Value == lane then
	else print("Aborted") end

If I replace the use of == lane with == 1, the correct value, it works just fine. Why can’t I use this variable in the if statement?

What is the lane, is it like a value or part??

Firstly, check if you are very sure that you sent lane == 1 from the client to the server, simply do this by:

storage.Lanes.PassScanner.OnServerEvent:Connect(function(player, lane)

Secondly, check your char.LaneAssignment.Value by simply print it out

And Lastly, just to make sure check your char.QueuePosition.Value by printing it out

1 Like

I’ve printed both out right before the if statement and they are correct- should satisfy the if statement.

QueuePosition and LaneAssignment are both number values.

then you need to do

char.LaneAssignment.Value == lane.Value

because if you are just comparing it to lane you are seeing if it is equal to the actual value instance.

I wish it was that easy :frowning: but lane is a variable coming from the remote event that when printed already equals 1.

Both sides of the equation equal 1 when printed out, yet the if statement is not seeing this as true…

Do an else statement and try printing the numbers you are comparing to each other side to side to see if they are the same at that point in the game.

Just did that, and at the else statement it’s still printing 1 for both sides of the equation.

Seems like you modified it before you posted it here, what does the original script look like if you modified it?

--pass through the scanner

storage.Lanes.PassScanner.OnServerEvent:Connect(function(player, lane)
	print("Got the pass signal " .. "for lane " .. lane .. ".")
	local char = script.Parent
	local test = lane
    print(char.LaneAssignment.Value .. lane)
		if char.QueuePosition.Value == 0 and char.LaneAssignment.Value == lane then
			local target = lanes[lane].PostScannerTarget
			local officer = player.Name
			game:GetService("Chat"):Chat(workspace[officer].Head, "You may pass through.", Enum.ChatColor.White)
			game:GetService("Chat"):Chat(script.Parent.Head, "Okay, coming through!", Enum.ChatColor.White)
			game:GetService("Chat"):Chat(script.Parent.Head, "That wasn't so bad...", Enum.ChatColor.White)
			lanes[lane].PaxInService.Value = false
		else print(char.LaneAssignment.Value .. lane) end
--		path.Blocked:Connect(function(blockedWpIdx)
--			if blockedWpIdx > wpIdx then
--			followPath(target)
--			end
--		end)

Try printing Queue position if you haven’t already.

1 Like

Maybe it’s just me not seeing it, but you didn’t declare the variable for “QueuePosition”

Maybe it’s somewhere unseen in the script but :person_shrugging:

No no QueuePosition and LaneAssignment are the two number value instances within the NPC I’m trying to command

The if statement works with the QueuePosition part that’s why I initially cut it out-

I only get problems with the use of the lane variable… replacing it with “1” literally fixes it so I don’t understand why a variable equaling 1 would not do the same thing.

Why do you need a variable declaring a number instead of just using the solution that you found?

Because the variable switches depending on what lane the NPC is assigned to (numbered 1 through 6)

Is your variable lane an array?

I don’t think so… it’s coming from a local script in a button inside of PlayerGUI. This is the script. Lane represents the name of the SurfaceGUI.

	local lane = script.Parent.Parent.Parent.name

Okay strangely enough I made the origin of the variable lane’s value (1) to be another number value which is equal to 1 and it worked.

Essentially I went from comparing a 1 which was a name of a ScreenGUI and a 1 from a number value to comparing a 1 from a number value and a 1 from a number value.

Can someone explain this? Is a number in the name of a ScreenGUI different from a number in the value of a number value?