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

function with a if bool seems to ignore the if statement?

So I have a script that when clicked, checks if a bool value is true or false. If its true then it prints "raisedtrue". If its false its prints "raisedfalse" however, when I run the script it just runs both at the same time and seems to ignore the if statement. does anyone know why?

local c = script.Parent.ClickDetector
local raised = script.Value.Value
c.MouseClick:Connect(function()
	if raised == true then
			raised = false
			print('raisedtrue')
	end
	if raised == false then
			raised = true
			print('raisedfalse')
	end
end)

1 answer

✓ Accepted answer

Those are two separate if statements. After one condition is checked for, the second if statement will also run, regardless of the success of the first one.

What's happening is raised is checked for being true, and if it is, it changes it to false. The next if statement runs because it is separate from the first, checks to see if it is false, and now that it is, it will then change it back to true.

To fix it, use elseif.

local c = script.Parent.ClickDetector
local raised = script.Value.Value
c.MouseClick:Connect(function()
	if raised == true then
		raised = false
		print('raisedtrue')
	elseif raised == false then
		raised = true
		print('raisedfalse')
	end
end)

This way, the second condition will be checked for if the first one is not met, not regardless of whether or not the first one was met.

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