How do I make the changed event work properly?

Hello, I am writing a script that takes off the player’s hat when they reach a certain amount of points. To check once they reached the amount they need I am using a changed event and I am a bit confused on how to use it properly in this situation.

script:

local Players = game:GetService("Players")
local function playerAdded(player)
	local function check(value)
	end
	player.leaderstats.Robux.Value.Changed:Connect(check)
	player.leaderstats.Robux.Value = 25
			player.CharacterAppearanceLoaded:Connect(function(character)
				local humanoid = character:WaitForChild("Humanoid")
				local accessories = {}
				humanoid:RemoveAccessories()
				wait(5)

				
				if player and player.Character and player.Character == character then
					local humanoid = character:FindFirstChildOfClass("Humanoid")
					if humanoid:GetState() ~= Enum.HumanoidStateType.Dead then
					
						for _, accessory in pairs(accessories) do 
							humanoid:AddAccessory(accessory)
						end
					end
				end

				accessories = nil
			end)
		end


		for _, player in pairs(Players:GetPlayers()) do 
			playerAdded(player)
		end
1 Like

The Changed event in a Value Object returns back the new value that it received as soon as the value changes from the server/client sides, you could just implement a couple of conditional checks if the Player has enough Robux to remove a specific hat:

local Players = game:GetService("Players")
local function playerAdded(player)
	local function check(value)
        if value >= 25 and player.Character:FindFirstChild("HatYouWantToRemove") then--Change this to whatever you want
            player.Character.HatYouWantToRemove:Destroy()
        end
	end
	player.leaderstats.Robux.Value.Changed:Connect(check)
	player.leaderstats.Robux.Value = 25
			player.CharacterAppearanceLoaded:Connect(function(character)
				local humanoid = character:WaitForChild("Humanoid")
				local accessories = {}
				humanoid:RemoveAccessories()
				wait(5)

				
				if player and player.Character and player.Character == character then
					local humanoid = character:FindFirstChildOfClass("Humanoid")
					if humanoid:GetState() ~= Enum.HumanoidStateType.Dead then
					
						for _, accessory in pairs(accessories) do 
							humanoid:AddAccessory(accessory)
						end
					end
				end

				accessories = nil
			end)
		end


		for _, player in pairs(Players:GetPlayers()) do 
			playerAdded(player)
		end

It still gives me this error “attempt to index number with ‘Changed’”.

Remove ‘.Value’, you have to use it on the value object.

Oh wait I see the issue

Value.Changed is not a valid property since you’re basically changing a property within a property, try changing that line to this:

	player.leaderstats.Robux.Changed:Connect(check)

Thanks that seemed worked perfectly!

1 Like