How do I check if a bool attribute is true or false with an if-statement?

The question is in the title.

Thanks for help!

3 Likes

try this (might not work)

local bool = (bool location)

if bool == true then
   (code)
else
   (code)
1 Like

This doesn’t work. I tried this a few minutes ago.

2 Likes

Yeah, then I do not know sorry

5 Likes

Did it error? This should work.

1 Like

Oh wait, it does work. I did something other wrong. Thanks for help

1 Like

Just to note, another way it can be done is

local bool = (bool location or a bool value you set)

if bool then
   --Code if true
else
   --Code if false
end

--Alternatively you can use not to check if the value is false or not

if not bool then --This checks if bool is false
   --Code
else --If not false then do what's after the else
   --Code
end

Both methods work fine so use what I or @Stickpuppet mentioned. Keep in mind my method will only work if you are only checking a single bool value and no other conditions

5 Likes

Try This:

if v:GetAttribute("Killable") == true then
end
1 Like

Hi.
I’m guessing I’m a little late but to check if an attribute is true, you could do this:

local Attribute = InstanceOfChoice:GetAttribute("AttributeName")

if Attribute and Attribute == true then
      -- The attribute is true
else
     -- The attribute is false
end

Hope this helps, even though it’s a year late.

2 Likes

Thanks for still taking your time to reply to this post, but I already figured out how to do that.

3 Likes

Thanks for the answer, helped me a lot with an attribute boolean.
This is what I got from it. Anyway have a good rest of your day/night.

local detector = script.Parent.ClickDetector

detector.MouseClick:Connect(function(player)
	if detector:GetAttribute("Boolean") == false then
	     detector:SetAttribute("Boolean", true)
             -- more code here
      end
end)
1 Like

Hey, replying to clarify something. Apparently :GetAttribute() == true can apply to two things: not only if the attribute is set to true, but also if it exists, and it seems like Studio always considers the latter. A probably stupid, but nevertheless reliable alternative to this is changing it to any other value type and checking for the desired one.

1 Like