Obby stage counter won't get value on player joining

I’m trying to make a stage counter for my obby, I have a different part of the script that updates when the value of the leaderstats changes, but it won’t pick up the value when the player joins. Help?

local stageCount = script.Parent
local stage = game.Players.LocalPlayer.leaderstats.stage

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild(“leaderstats”)
stageCount.BottomText.Text = leaderstats.stage.Value
end)

You need a datastore roblox does not save data by default

you can also use other datastore alternatives like profile service or datastore 2

I’m already using a datastore, my problem is that when the player joins, it’s not reading the value that is saved

Oh okay welp I guess cant really help you because I use profile service instead of datastore hopefully someone who does can come and help you.

I don’t think it’s a datastore issue, but I’m probably wrong

Maybe from your leaderstat script you can fire a remote event when the player joins of the value of the stage because I’m pretty sure you can’t access leaderstats value from local script? Im not sure Im probably wrong its been a while since I used leaderstats

  1. The leaderstats is not guaranteed to exist when the LocalScript runs.
  2. You’ll already be in the game before PlayerAdded gets fired.
  3. You are using the wrong player variable in your example, as you are updating the stage value by the latest player’s stage with this logic.
local Players = game:GetService("Players")

-- get LocalPlayer and wait for data
local LocalPlayer = Players.LocalPlayer
local leaderstats = LocalPlayer:WaitForChild("leaderstats")
local stageData = leaderstats:WaitForChild("stage")
local stageCount = script.Parent

-- function to update stage count gui
local function updateStageCount()
    stageCount.BottomText.Text = stageData.Value
end

-- update stage count the first time
updateStageCount()

-- update stage count whenever the stageData changes
stageData.Changed:Connect(updateStageCount)

This all assumes you have a proper leaderstats setup.

Thanks for the solution! I’m newer to scripting so this helps!

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