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

How to stop the If statement picking the last option?

I made a script that detects if the players win or the Hunter wins but no matter what I do, the If statement always picks the last option.

The script

local ingamePlayersFolder = game.Workspace.ingamePlayers:GetChildren()
local winnersFolder = game.Workspace.Winners:GetChildren()

if #winnersFolder >= 1 or #ingamePlayersFolder >= 1 then
				stats.Value = "Players wins!"
				print("Players wins!")
			elseif #winnersFolder >= 1 and #ingamePlayersFolder >= 1 then
				stats.Value = "Players wins!"
				print("Players wins!")
			elseif #winnersFolder < 1 and #ingamePlayersFolder < 1 then
				stats.Value = "Hunter wins!"
				print("Hunter wins!")
			end

2 comments

0

remove the last one

0

I tried doing that but I didn't get any print message or the stats value changing after I removed the last one.

1 answer

✓ Accepted answer

Hi D4_rrk,

The first and second selections (if and elseif) will give identical answers, as if the 1st elseif is true, the 1st if MUST have been true. I hope this code helps you understand why.

val1 = true
val2 = true

if val1 or val2 then
	print("Yes")
elseif val1 and val2 then -- If both are true, one of them must have already been true, so this never gets done.
	print("Yes")
end

So the 1st elseif is completely useless in this scenario, simply adding more clutter to your script.

I was able to emulate the player and hunter win, putting an object in either of the folders gave me a player win.

Hope this helped. Please reply if there are still issues.

5 comments

0

I'm a little bit confused on what you mean by val1 and val2, since I need the script determine if the players win or if the Hunter wins from the 2 folders that I have.

0

So, in an if selection, where you use elseifs as well, it goes down the list from the top to bottom. An if statement looks at what you give it, and if it is true, then it runs it's code, otherwise it moves on to the next one. Val1 and Val2 are in place for your comparing of the two numbers. #winnersFolder returns a number, which is compared for a true or false. This is what val1 and val2 represent

0

So, in an if selection, where you use elseifs as well, it goes down the list from the top to bottom. An if statement looks at what you give it, and if it is true, then it runs it's code, otherwise it moves on to the next one. Val1 and Val2 are in place for your comparing of the two numbers. #winnersFolder returns a number, which is compared for a true or false. This is what val1 and val2 represent

0

It works now, thanks for the help!

0

No problem.

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