Server not giving leaderstat value

Hello!

I have got a daily rewards system in my game. Where everyday you join you get some credits. But for some reason it does not give any credits.

I have also added a breakpoint and it said that Credits.Value is 0 which is weird because it saves at exactly 100.

All help is appreciated:)

local DataStoreService = game:GetService("DataStoreService")
local DailyRewardsDatastore = DataStoreService:GetDataStore("DailyRewardsDatastore")

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAppearanceLoaded:Wait()
	print(plr.Name)
	local LastDayClaimed = Instance.new("NumberValue", plr)
	LastDayClaimed.Name = "LastDayClaimed"
	LastDayClaimed.Value = 0

	local DailyRewardsMult = Instance.new("NumberValue", plr)
	DailyRewardsMult.Name = "DailyRewardsMult"
	DailyRewardsMult.Value = 1.5

	
	local Key = tostring(plr.UserId)
	local CurrentDate = tonumber(os.date("%j"))
	local BaseValue = 10
	local retryCount = 0
	local maxRetries = 5

	-- GUI Elements
	local DailRewardsGUI = plr.PlayerGui:WaitForChild("DailyRewardsGUI")
	local NameText = DailRewardsGUI.MainFrame.NameText
	local AmountText = DailRewardsGUI.MainFrame.Amount
	local ContinueBTN = DailRewardsGUI.MainFrame.ContinueBTN

	DailRewardsGUI.Enabled = false

	local Credits = plr:WaitForChild("leaderstats"):WaitForChild("Credits")
	
	-- Function to handle reward logic
	local function ClaimDailyRewards()

		if CurrentDate >= LastDayClaimed.Value then
			-- Update values and save to datastore
			LastDayClaimed.Value = CurrentDate
			local rewardAmount = BaseValue * DailyRewardsMult.Value
			Credits.Value += rewardAmount

			-- Update the GUI with reward information
			AmountText.Text = "+ " .. rewardAmount .. " CREDITS!"
			DailRewardsGUI.Enabled = true

			print("Daily reward claimed: " .. rewardAmount)

			-- Save to DataStore
			local DataTable = {LastDayClaimed.Value}
			DailyRewardsDatastore:SetAsync(Key, DataTable)
		else
			print("Daily reward already claimed today.")
		end
	end

	-- Attempt to load DataStore
	local success, errorMessage = pcall(function()
		local Data = DailyRewardsDatastore:GetAsync(Key)
		if Data then
			LastDayClaimed.Value = Data[1]
		end
	end)

	while not success and retryCount < maxRetries do
		retryCount += 1
		task.wait(3)
		print("Retrying to load DataStore... Attempt " .. retryCount)
		success, errorMessage = pcall(function()
			local Data = DailyRewardsDatastore:GetAsync(Key)
			if Data then
				LastDayClaimed.Value = Data[1]
			end
		end)
	end

	if not success then
		warn("Failed to load DataStore for player " .. plr.Name .. " after " .. maxRetries .. " attempts: " .. errorMessage)
		return
	end

	-- Check and claim rewards
	ClaimDailyRewards()

	-- GUI Interaction
	ContinueBTN.MouseButton1Click:Connect(function()
		DailRewardsGUI.Enabled = false
	end)
end)

So have you determined the specific line that results in the error? Print the value just before and after you set it. There’s a decent chance that either the code isn’t running, or maybe you are trying to access a different value.

It does recognise Credits but when I print it it says the value is 0

And you had it print the value the line after you set it?

Yes I did.


Did you also print the RewardAmount?

Yes I did and it printed out the correct reward amount which is 15.

Send the updated code and a screenshot of the console.

local DataStoreService = game:GetService("DataStoreService")
local DailyRewardsDatastore = DataStoreService:GetDataStore("DailyRewardsDatastore")

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAppearanceLoaded:Wait()
	print(plr.Name)
	local LastDayClaimed = Instance.new("NumberValue", plr)
	LastDayClaimed.Name = "LastDayClaimed"
	LastDayClaimed.Value = 0

	local DailyRewardsMult = Instance.new("NumberValue", plr)
	DailyRewardsMult.Name = "DailyRewardsMult"
	DailyRewardsMult.Value = 1.5

	
	local Key = tostring(plr.UserId)
	local CurrentDate = tonumber(os.date("%j"))
	local BaseValue = 10
	local retryCount = 0
	local maxRetries = 5

	-- GUI Elements
	local DailRewardsGUI = plr.PlayerGui:WaitForChild("DailyRewardsGUI")
	local NameText = DailRewardsGUI.MainFrame.NameText
	local AmountText = DailRewardsGUI.MainFrame.Amount
	local ContinueBTN = DailRewardsGUI.MainFrame.ContinueBTN

	DailRewardsGUI.Enabled = false

	local Credits = plr:WaitForChild("leaderstats"):WaitForChild("Credits")
	if Credits then
		print(Credits.Value)
		
	end
	-- Function to handle reward logic
	local function ClaimDailyRewards()

		if CurrentDate >= LastDayClaimed.Value then
			-- Update values and save to datastore
			LastDayClaimed.Value = CurrentDate
			local rewardAmount = BaseValue * DailyRewardsMult.Value
			print(rewardAmount)
			Credits.Value += rewardAmount

			-- Update the GUI with reward information
			AmountText.Text = "+ " .. rewardAmount .. " CREDITS!"
			DailRewardsGUI.Enabled = true

			print("Daily reward claimed: " .. rewardAmount)

			-- Save to DataStore
			local DataTable = {LastDayClaimed.Value}
			DailyRewardsDatastore:SetAsync(Key, DataTable)
		else
			print("Daily reward already claimed today.")
		end
	end

	-- Attempt to load DataStore
	local success, errorMessage = pcall(function()
		local Data = DailyRewardsDatastore:GetAsync(Key)
		if Data then
			LastDayClaimed.Value = Data[1]
		end
	end)

	while not success and retryCount < maxRetries do
		retryCount += 1
		task.wait(3)
		print("Retrying to load DataStore... Attempt " .. retryCount)
		success, errorMessage = pcall(function()
			local Data = DailyRewardsDatastore:GetAsync(Key)
			if Data then
				LastDayClaimed.Value = Data[1]
			end
		end)
	end

	if not success then
		warn("Failed to load DataStore for player " .. plr.Name .. " after " .. maxRetries .. " attempts: " .. errorMessage)
		return
	end

	-- Check and claim rewards
	ClaimDailyRewards()

	-- GUI Interaction
	ContinueBTN.MouseButton1Click:Connect(function()
		DailRewardsGUI.Enabled = false
	end)
end)

image_2025-03-10_105128248

the 0 is when I printed the Credits.Value and the 15 is when I printed the RewardsAmount

So the code fetching the value prints before you update it? Everything seems to be working fine. This is why I said to print the value after you set it. You also should not be updating UI from the server. Only update their Credits, do the rest from a local script within the ui.

Ok when I printed the Credits it came up with 15.

Which is wierd since it should come up with 115 which the current credits I have right now is 100 (100+15 = 115)

What says you have 100 credits? Print the value of credits after you set it.

I did print the credits value and it said 0, but in my leaderstat it says 100

Send the code and the output please. For everything UI related, you should really move it to a LocalScript, decent chance you are having some issues from it.

I will move everything ui related to the server side. Thanks for your help!

this is a little unrelated, but wouldn’t this break from December 31st to January 1st?