How can I detect if the value of the leaderstat changes?

Hello,
I want to make a puzzle game and it should teleport the player to the next level if the box touches its goal. but I have this problem: I don’t know how to detect if the leaderstat changes. Does anyone know how to detect it? I did it like this but it didn’t work:

player.leaderstats.Level.Value.Changed:Connect(function()
print("works")
end)

any help is appreciated!

Am not sure is this corrected and matched for you yet
To detect changes in a Leaderstat value in Roblox using a script, you need to make sure the Leaderstats have replicated storage enabled. Here’s an example of how you can detect changes in a Leaderstat value and print a message when it changes:

local player = game.Players.LocalPlayer

-- Function to check for changes in the Leaderstat value
local function onLeaderstatChanged(property)
    if property == "Value" then
        print("Leaderstat value changed to: " .. player.leaderstats.Level.Value)
        
        -- Add your logic here to teleport the player to the next level if needed
    end
end

-- Connect the function to the Changed event of the Leaderstat value
player.leaderstats.Level:GetPropertyChangedSignal("Value"):Connect(onLeaderstatChanged)

Make sure to place this script in a LocalScript within the player’s PlayerGui or StarterPlayerScripts so it runs on the client side.

This script will monitor changes in the “Value” property of the Level Leaderstat for the player. When the value changes, it will print the new value to the output window. You can then add your logic to teleport the player to the next level based on the Leaderstat value change.

From what the code does, I would guess that you read something like attempt to index number with "Changed" in the output. To understand what an error like that means, let’s break it down:

  1. Firstly, we need to know what indexing is. Tables contain data (surprise surprise) that can be retrieved or overwritten according to unique identifiers called ‘indices’ (singular ‘index,’ ‘key’ is synonymous as far as I’m concerned in Lua, though some might be nitpicky about the difference). Indexing is simply the process of retrieving data at specified indices.
  2. Instances (such as IntValue objects) behave similarly to tables in Luau such that you can retrieve properties and children through indexing. This is exactly what you did with the first part of the script:
  1. But something isn’t quite right here. Value is a property of IntValue that always equates to an integer, not a table. Thus, attempting to index Value will throw an error.
  2. If this was unintentional, you probably already know the fix! If you don’t, it’s very simple: the IntValue object itself possesses the event Changed, not its numeric property Value. Since you probably already know that you can retrieve events via indexing, all you have to do is simply index the object directly:
player.leaderstats.Level.Changed:Connect(function()
    print'works';
end)

This snippet of code will subscribe a function to the Changed event so long as the Level value object exists at runtime. To foolproof the code, you can call WaitForChild to ensure ‘leaderstats’ and ‘Level’ exist at runtime:

player:WaitForChild'leaderstats':WaitForChild'Level'.Changed:Connect(function()
    print'works';
end)