How to get this value?

Hey guys. Recently, I started taking advantage of “attributes” and came to realize how handy they are. However, I can’t seem to get the value of an attribute. I’ve tried everything I know of, but nothing I try is able to get the value of the attribute! Any way I can fix this?

First idea:

local Model = script.Parent
local RotScript = script.Parent.RotScript
local TimeBeforeRot = Model:GetAttribute("TimeBeforeRot")
if TimeBeforeRot == 0 then
    RotScript.Enabled = true
end

Second idea:

local Model = script.Parent
local RotScript = script.Parent.RotScript
local TimeBeforeRot = Model:GetAttribute("TimeBeforeRot")
if TimeBeforeRot == Model:SetAttribute("TimeBeforeRot", 30) then
    RotScript.Enabled = true
end

None of these worked. Thanks a bunch if you decide to help me!

Hi so :SetAttribute() Applies a new value to an instance.
Like this for example part:SetAttribute("Coins", 50)
This will apply a attribute to the part named “Coins” and set the value to 50.

:GetAttribute() Will read the attribute value on an instance. So you use this to get the value of an attribute.
Like this for example part:GetAttribute("Coins") -- Returns 50
if :GetAttribute("Coins") Doesn’t exist it will return nil

What the issue could be is that Model:GetAttribute("TimeBeforeRot") doesn’t exist, so it returns nil. Could you try to do print(Model:GetAttribute("TimeBeforeRot")) and see if it prints nil or a value?

1 Like

You should only use the first idea.

local Model = script.Parent
local RotScript = script.Parent.RotScript
local TimeBeforeRot = Model:GetAttribute("TimeBeforeRot")
if TimeBeforeRot == 30 then
     RotScript.Enabled = true
end

Okay sure. I’ll try that as soon as I can

Don’t create a variable if you’re only using it once (and in the case of attributes, you’ll most likely keep getting them), for performance reasons.

if Model:GetAttribute("TimeBeforeRot") == 30 then
..
end

your setting the attribute, you should be getting the attribute.

Sorry for the wait. This did work though! Special thanks to all of you who considered me enough to try and help. :grinning:

1 Like

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