Attribute change not being detected?

Hello, I’m currently making a statistics system where you can upgrade your power, endurance, stamina, and other stats. I have recently found out about “Attributes” and decided to use it.

This issue is applicable to every attribute excluding skill points. The Jump, Max Power, Delay, and Glide stats are detected on the client but only prints their starting value, not their value after the changes.

Server Script:

Players.PlayerAdded:Connect(function(Player)
	local statFolder = Instance.new("Folder")
	statFolder.Name = "Stats"
	statFolder.Parent = Player
	
	statFolder:SetAttribute("Jump", 0)
	statFolder:SetAttribute("Delay", 0)
	statFolder:SetAttribute("Glide", 0)
	statFolder:SetAttribute("MaxPower", 8)
	statFolder:SetAttribute("Endurance", 10)
	statFolder:SetAttribute("SkillPoints", 20)
	
end)

statEvent.OnServerEvent:Connect(function(plr, input, value)
	local StatFolder = plr:WaitForChild("Stats")
	
	local function limit(stat, name, val, val2)
		if stat > val then stat = StatFolder:SetAttribute(name, val) elseif stat < val2 then StatFolder:SetAttribute(name, val2) end
	end
	
	if input == "Jump" then
		StatFolder:SetAttribute("Jump", value)
		limit(StatFolder:GetAttribute("Jump"), "Jump", 6, 0)
		
		
	elseif input == "Power" then
		StatFolder:SetAttribute("MaxPower", value)
		limit(StatFolder:GetAttribute("MaxPower"), "MaxPower", 17, 8)
		
		
	elseif input == "SkillPoints" then
		StatFolder:SetAttribute("SkillPoints", StatFolder:GetAttribute("SkillPoints") + value)
		limit(StatFolder:GetAttribute("SkillPoints"), "SkillPoints", 20, 0)
		
		
	elseif input == "Endurance" then
		StatFolder:SetAttribute("Endurance", value)
		limit(StatFolder:GetAttribute("Endurance"), "Endurance", 15, 10)
		
		
	elseif input == "Delay" then
		StatFolder:SetAttribute("Delay", value)
		limit(StatFolder:GetAttribute("Delay"), "Delay", 0.925, 0)
		
		
	elseif input == "Glide" then
		StatFolder:SetAttribute("Glide", value)
		limit(StatFolder:GetAttribute("Glide"), "Glide", 0.35, 0)
		
	end
end)

Here is a part of a LocalScript that handles power. This is also where the issue happens, the print(maxPower) only prints 8 even when it detects attribute changes and increases. This also happens with the Jump, Glide, and Delay stats.

	statFolder:GetAttributeChangedSignal("MaxPower"):Connect(function()
		print(maxPower) -- always prints 8 even if the attribute is increasing.
		Label.Text = power.Value.." / "..maxPower
		Bar.Size = UDim2.new(power.Value/maxPower, 0, 1, 0)
	end)

If I am doing something wrong, please tell me.

ADDITIONAL INFORMATION:
I have also tried attributes for detecting the player’s tilt type, spin value, ball type, and they all seem to work and become recognized.

It’s probably because you’re setting maxPower inside of a variable instead of getting it directly, change that last code to this

statFolder:GetAttributeChangedSignal("MaxPower"):Connect(function()
	local newVal = statFolder:GetAttribute("MaxPower")
	print(newVal)
	Label.Text = power.Value.." / "..newVal
	Bar.Size = UDim2.new(power.Value/newVal, 0, 1, 0)
end)

The one I showed is just a part of a script, the value of the variable always changes when pressing E or Q to increase or reduce power. So it should work regardless. :slight_smile:

May I see how you’re setting the value in the variable? Also I’m not sure why you’re even doing it externally when getting it from the Attribute directly would be less risky and more efficient, but to each their own, it’s probably an issue with the way the variable is changing

local Player = game.Players.LocalPlayer
local char = Player.Character or Player.CharacterAdded:Wait()
local Bar = script.Parent.Frame
local Label = script.Parent.TextLabel
local ts = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(0.25, Enum.EasingStyle.Quint, Enum.EasingDirection.Out, 0, false, 0)
local goal = {}
local u3 = nil

local statFolder = Player:WaitForChild("Stats")

local function HandleChar(char)
	local power = char:WaitForChild("Power")
	local maxPower = statFolder:GetAttribute("MaxPower")
	
	Bar.Size = UDim2.new(power.Value/maxPower, 0, 1, 0)
	Label.Text = power.Value.." / "..maxPower
	
	statFolder:GetAttributeChangedSignal("MaxPower"):Connect(function()
		print(maxPower)
		Label.Text = power.Value.." / "..maxPower
		Bar.Size = UDim2.new(power.Value/maxPower, 0, 1, 0)
	end)
	
	power.Changed:Connect(function()
		Label.Text = power.Value.." / "..maxPower
		goal.Size = UDim2.new(power.Value / maxPower, 0, 1, 0);
		u3 = ts:Create(Bar, tweenInfo, goal)
		u3:Play()
	end)
	
end

if Player.Character then
	HandleChar(Player.Character)
end

Player.CharacterAdded:Connect(HandleChar)

Here you go.

1 Like

Well from what I can see from thr code you’ve provided, maxPower is only set once when the Character respawns, hence why it’s the same. Here’s an exmaple of what’s going on

  • Character respawns
  • maxPower is set to the current value of the MaxPower Attribute
  • MaxPower Attribute was changed but instead of referencing the current value of the Attribute, it references the value of the Attribute that was set to the variable maxPower
  • Same thing applies to power.Changed since it uses the maxPower variable too

you need to get the current Attribute value yourself via

maxPower = statFolder:GetAttribute("MaxPower")

In order to get the latest value for both events, that’s why it’s not changing, it’s set once when the character respawns and is never updated

I’ll try that. It seems that I actually have not read through the code correctly when transitioning to attributes. I’ll make sure to let you know after I do it.

1 Like

I wish you good luck, it should be a simple thing to do since it’s just changing maxPower when power is changed or MaxPower is changed via the line I’ve provided

Also @123dsadqwe that’s how I first proposed the fix for him lol

1 Like

Try this

Players.PlayerAdded:Connect(function(Player)
	local statFolder = Instance.new("Folder")
	statFolder.Name = "Stats"
	statFolder.Parent = Player
	
	statFolder:SetAttribute("Jump", 0)
	statFolder:SetAttribute("Delay", 0)
	statFolder:SetAttribute("Glide", 0)
	statFolder:SetAttribute("MaxPower", 8)
	statFolder:SetAttribute("Endurance", 10)
	statFolder:SetAttribute("SkillPoints", 20)

	statFolder:GetAttributeChangedSignal("MaxPower"):Connect(function()
        local maxPower = statFolder:GetAttribute("MaxPower")
		print(maxPower) -- always prints 8 even if the attribute is increasing.
		Label.Text = power.Value.." / "..maxPower
		Bar.Size = UDim2.new(power.Value/maxPower, 0, 1, 0)
	end)

end)

Sorry for the late reply, but the solution worked! I had to do it for the other statistics as well. Thank you for the solution! :slight_smile:

1 Like

Anytime! If you have anymore issues don’t be afraid to make another post!

1 Like

Thank you for the answer, that will work too. :slight_smile: