Update frame when an attribute changes

how can I check whether the player’s attribute has changed? I did this using while wait, but as a result of this, many frames with one player are cloned.

local function prisons()
	for i,plr in Players:GetPlayers() do
while wait (0.05) do
		if plr:GetAttribute("WantedReason").Changed ~= "" then
		local example = scrollingFrame.FrameButton
		local framebutton = example:Clone()
		framebutton.Parent = scrollingFrame
		framebutton.Name = "Prison"
		framebutton.NamePlr.Value = plr.Name
		framebutton.PlrName.Text = plr.Name
		framebutton.PlrWanted.Text = plr:GetAttribute("WantedReason")
		print(framebutton.NamePlr.Value)
		framebutton.Visible = true
		task.wait(0.1)
end
		end
	end    
end
prisons()

Players.PlayerAdded:Connect(function()
	for index, value in pairs(script.Parent:GetChildren()) do
		if value.Name == "Prison" then
			value:Destroy()
		end
	end
	prisons()
end)

Use :GetAttributeChangedSignal(attributeName) then attach a :Connect() to it.
i.e.

plr:GetAttributeChangedSignal("WantedReason"):Connect(function()
    if plr:GetAttribute("WantedReason").Changed == "" then
        return
    end

    -- do whatever
end)

The problem with your code is that
plr:GetAttribute("WantedReason") returns the attribute’s value, not the attribute.

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