Issue with attributes on the client, (GetAttribute only retrieving original / old value and not new one)

The issue I’m having is with attributes (client side) when using the GetAttribute function the thing I want it to do is, I want the player to set a new value to a attribute and then have it GetAttribute (in a different localscript) for a calculation, the thing it does is get the old value and doesn’t get the new one that was set to it.

Example:
I test the game with attribute being 10, when I set it to 200
its gonna print 10 in a different script even though its 200,
GetAttribute works as expected in the commandbar but not the script itself

What the script is in one of the text buttons that sets the value for the attribute

local textBox = script.Parent


local colorNormal = Color3.new(1, 1, 1) -- white
local colorWrong = Color3.new(1, 0, 0) -- red
local colorCorrect = Color3.new(0, 1, 0) -- green

-- Initialize the state of the textBox
textBox.ClearTextOnFocus = true
textBox.Text = ""
textBox.Font = Enum.Font.Code
textBox.PlaceholderText = "insert salary here"
textBox.BackgroundColor3 = colorNormal

local function onFocused()
	textBox.BackgroundColor3 = colorNormal
end

local allocated_pol_budget = script.Parent.Parent:GetAttribute("pol_budget")
local allocated_mil_budget = script.Parent.Parent:GetAttribute("mil_budget")
local allocated_college_budget = script.Parent.Parent:GetAttribute("college_budget")
local ROMES_GDP = game:GetService("ReplicatedStorage").Economy.GLOBAL_VALUES.ROMES_GDP.Value

local function onFocusLost(enterPressed, inputObject)
	if enterPressed then
		local guess = textBox.Text
		
		local total_value = tonumber(guess) + allocated_mil_budget + allocated_college_budget
		print(allocated_pol_budget,allocated_mil_budget,allocated_college_budget,total_value)

		if total_value < ROMES_GDP then
			textBox.Text = "Entered"
			textBox.BackgroundColor3 = colorCorrect
			script.Parent.Parent:SetAttribute("pol_budget", tonumber(guess))
		else
			textBox.Text = "Over budget"
			textBox.BackgroundColor3 = colorWrong
		end
	else
		-- The player stopped editing without pressing Enter
		textBox.Text = ""
		textBox.BackgroundColor3 = colorNormal
	end
end

textBox.FocusLost:Connect(onFocusLost)
textBox.Focused:Connect(onFocused)

I don’t really see any part of the script that changes the attribute of the code. Could you show it?

		script.Parent.Parent:SetAttribute("pol_budget", tonumber(guess))

This is the part that sets the new value

I haven’t used attributes before, but your code is only looking at the Reference you stored your attributes value in at the start of the script.

You need to GetAttribute again to receive the updated value.

local attribute = 1

local b = attribute

attribute += 1

print ( b ) -- prints 1, as that is the value of attributes when you created the variable “b”.
print ( attribute ) -- 2
1 Like

I get what you mean and tried this, which didn’t work

local textBox = script.Parent


local colorNormal = Color3.new(1, 1, 1) -- white
local colorWrong = Color3.new(1, 0, 0) -- red
local colorCorrect = Color3.new(0, 1, 0) -- green

-- Initialize the state of the textBox
textBox.ClearTextOnFocus = true
textBox.Text = ""
textBox.Font = Enum.Font.Code
textBox.PlaceholderText = "insert salary here"
textBox.BackgroundColor3 = colorNormal

local function onFocused()
	textBox.BackgroundColor3 = colorNormal
end

local allocated_pol_budget_get = script.Parent.Parent:GetAttribute("pol_budget")
local allocated_mil_budget_get = script.Parent.Parent:GetAttribute("mil_budget")
local allocated_college_budget_get = script.Parent.Parent:GetAttribute("college_budget")
local ROMES_GDP = game:GetService("ReplicatedStorage").Economy.GLOBAL_VALUES.ROMES_GDP.Value

local function onFocusLost(enterPressed, inputObject)
	if enterPressed then
		local guess = textBox.Text
		-- added this
		local allocated_pol_budget = allocated_pol_budget_get
		local allocated_mil_budget = allocated_mil_budget_get
		local allocated_college_budget = allocated_college_budget_get 

		local total_value = tonumber(guess) + allocated_mil_budget + allocated_college_budget
		print(allocated_pol_budget,allocated_mil_budget,allocated_college_budget,total_value)

		if total_value < ROMES_GDP then
			textBox.Text = "Entered"
			textBox.BackgroundColor3 = colorCorrect
			script.Parent.Parent:SetAttribute("pol_budget", tonumber(guess))
allocated_pol_budget = tonumber(guess) -- this for redudancy
		else
			textBox.Text = "Over budget"
			textBox.BackgroundColor3 = colorWrong
		end
	else
		-- The player stopped editing without pressing Enter
		textBox.Text = ""
		textBox.BackgroundColor3 = colorNormal
	end
end

textBox.FocusLost:Connect(onFocusLost)
textBox.Focused:Connect(onFocused)

This just returns the value of it, so it isn’t being updated whatsoever. So allocated_pol_budget_get will just be the literal number 10.

Thats why I have this line to set it to the new value. (This is the line that sets it to 200 but the line you mentioned would still return 10)

script.Parent.Parent:SetAttribute("pol_budget", tonumber(guess))

You need to update these everytime you call the function otherwise they will always be the old value because they’re outside the function meaning they only got ran once and saved the previous value but never ran again so they remain whatever was previously in the attributes, easiest solution is to just put those three inside the function when player presses enter

2 Likes

My bad was updating it incorrectly.