Why is this condition being met?

My goal is to ONLY fire my “beginCapturing” function if the conditions:

  • cpVal7 == “nil” or “axis”
  • axisVal7.Value == 0
  • allieVal1.Value > 1

My issue is that my conditions seem to be “met” when they supposedly shouldn’t be met, here’s an image of the output during my testing, always firing the “beginCapturing” function:
image

Here’s my function, containing the conditions:

allieVal7:GetPropertyChangedSignal("Value"):Connect(function()
	task.wait(1.5)
	print(axisVal7.Value, "axis val")
	print(allieVal7.Value, "allies val")
	if cpVal7.Value == "nil" or cpVal7.Value == "axis" and axisVal7.Value == 0 and allieVal1.Value > 1 then
		print("beginning allies capture, there are this many axis in:", axisVal7.Value)
		beginCapturing("allies", trench7)
	elseif cpVal1.Value == "nil" or cpVal1.Value == "allies" and axisVal1.Value > 0 and allieVal1.Value == 0 then
		print("beginning axis capture, there are this many ally in:", allieVal7.Value)
		beginCapturing("axis", trench7)
	end
end)

Context:

  • allieVal7’s value updates are based off of how many player’s on the “allies” team are in a specific area.

  • axisVal7 is the same but for the “axis” team.

  • cpVal17.Value == “nil” is always true right now, as the functionality to change it isn’t even in the game yet.


Note:

  • The “task.wait(1.5)” was added previous to the print statements to check if my issue was with the runtime, I don’t think anything changes when I increase or decrease this though

  • I’ve tried searching around, however, my problem is kind of specific and I haven’t got any responses elsewhere.

Try something like this, by changing the order.
Capturewho is your equivalent of cpval

if axis == 0 and allies > 1 and captureWho == nil or captureWho == "axis" then
    print("b")
end

Here are the values I ran with

local axis = 1
local allies = 0
local captureWho = nil

Hopefully this helps.

1 Like

I’ll give this a shot, thank you!

and has a higher operator precedance than or. operator precedence is PEMDAS for programmers.
Group your conditions with parentheses.

if (cpVal7.Value == "nil" or cpVal7.Value == "axis") and ....
2 Likes

I’ll try this as well, JUST learned this in school lol… can’t believe I forgot, thank you!

1 Like

Alright, so I set my conditions up with brackets and now I’m having another issue…

Here’s what the current code looks like:

allieVal7:GetPropertyChangedSignal("Value"):Connect(function()
	task.wait(1)
	print(cpVal7.Value, axisVal7.Value, allieVal7.Value)
	if (cpVal7.Value == "nil" or cpVal7.Value == "axis") and (axisVal7.Value == 0 and allieVal1.Value > 0) then
		print(axisVal7.Value, "axis val")
		print(allieVal7.Value, "allies val")
		print("beginning allies capture, there are this many axis in:", axisVal7.Value)
		beginCapturing("allies", trench7)
	elseif (cpVal1.Value == "nil" or cpVal1.Value == "allies") and (axisVal1.Value > 0 and allieVal1.Value == 0) then
		print(axisVal7.Value, "axis val")
		print(allieVal7.Value, "allies val")
		print("beginning axis capture, there are this many ally in:", allieVal7.Value)
		beginCapturing("axis", trench7)
	end
end)

That print statement above the conditions returned
cpVal17.Value as nil,
axisVal7.Value as 0,
allieVal7.Value as 1,

however, now the condition is meeting at all… opposite problem.

Most things in this script are using *Val7, except for the highlighted locations. Is that intentional?

1 Like

There’s the problem, I cannot fathom how I missed that, thank you and god bless your eyeballs.

Works perfectly now.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.