Why GetAttributeChangedSignal Only Run Once?

Can anyone explain to me why this only run once? It supposed to fire every time the attribute changes, correct? The attribute is given by the server when you join, and I’ve made sure the name is correct. It does print the “newSpeed” when you join but when it changes again it just stops. Adding a loop doesn’t work.

local plr = game.Players.LocalPlayer
local Character = plr.Character or plr.CharacterAdded:Wait()
local newSpeed = Character:GetAttribute("RunSpeed")

print(Character)

if Character:GetAttributeChangedSignal("RunSpeed") then
	newSpeed = Character:GetAttribute("RunSpeed")
	print(newSpeed)
end
2 Likes

Try turning :GetAttributeChangedSignal() it into a function. That way, it should run every time the attribute is changed.

Character:GetAttributeChangedSignal("RunSpeed"):Connect(function()
	newSpeed = Character:GetAttribute("RunSpeed")
	print(newSpeed)
end)
3 Likes

Sure. You’re not binding that RBXScriptSignal to anything, and it’s truthy in a boolean context, so it passes the if condition.

1 Like

What do you mean? Isn’t it connect to a function?

Can you show me an example of what that would look like?

Documentation is your best friend:

Just like your plr.CharacterAdded signal, GetAttributeChangedSignal() returns an RBXScriptSignal that fires whenever the passed string changes.

You then Connect that signal to a function like how @J_Angry provided.

2 Likes

OHHH, I’m actually slow. Thank you so much. I can’t believe I overlook connect function.

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