Script can't find boolean attributes

Hello, fellow people! So, I was scripting my gun system (just started it) and I encountered a problem. There is a Boolean attribute inside of the gun (model) named IsReloading. And basically the script has a part where if it can’t find any of the given attributes (in a table) it throws out an error and returns nil. I do have the attribute inside of the gun, but it’s skipping it. When I make the IsReloading attribute any other, for example, string or number. It perfectly works. It might be a bug, but I’m not sure. I could use folders instead of attributes which isn’t a problem, but I want this problem to be spotted by developers.

Module Script:

--// Tables
local _Weapon = {}

--// Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--// ReplicatedStorage Variables
local Weapons = ReplicatedStorage:WaitForChild("Weapons")

--// Main functions
function _Weapon:InitializeWeapon(weapon : Model)
	local AttributesRequired = {
		[1] = "WeaponType"; -- Weapon type (Pistol, Automatic, Semi-Automatic, Shotgun, Sniper)
		[2] = "Recoil"; -- Vector3 value
		[3] = "MaxBullets"; -- Max bullets you can have at a time
		[4] = "CurrentAmmoAmount"; -- Current ammo
		[5] = "IsReloading"; -- Will be set to true if you're reloading the gun at the moment.
	}

	for i, v in pairs(AttributesRequired) do
		if not weapon:GetAttribute(v) then
			warn(string.format(("There isn't a(n) %s attribute in %s."), v, weapon.Name))
			return nil
		end
	end
	
	print("All perfect!")
end

--// Other
return _Weapon

Main script (that I used for testing):

local _Weapon = require(game.ReplicatedStorage.Modules:WaitForChild("_Weapon"))

_Weapon:InitializeWeapon(game.ReplicatedStorage.Weapons.Model)

A picture that shows the attributes:

Thank you for reading!

This happens because :GetAttribute() returns the attribute value, so technically you’re checking if the IsReloading attribute exists OR equals true.

To fix this, you need to set a variable to the attribute value and then check if that attribute first exists.

local attr = weapon:GetAttribute(v)
if attr == nil then -- If the attribute is set to 'false' then the 'if attribute then' will fail.
  warn("Failed to find attribute")
end
3 Likes

Okay I’ll try that! Thank you!

No problem! Lemme know if it doesn’t work

Thank you so much! That worked! (I can’t believe I wasted 2 hours on that lol…)

No problem! If you want more information just read the wiki page on Attributes.

1 Like