Trouble connecting DataStore Information to Player GUI TextLabel

Hello there,

I have an issue with the datastore system in my daily-reward arrangement, by which the textlabel for the local player will not change according to the time remaining in my server-side script, and there are no errors in my output panel. It should work as I have the necessary remoteevents required to fulfil the operation.

To illustrate my situation, my output depicts the following:

Time since last claim 15502 - Server - Events:290

While the blank text (“…”) does not portray anything related to this value:

I have used AlvinBlox’s “How To Make A Daily Reward” video for reference.

Here are my scripts for critique:

Server

local RewardsDataStore = game:GetService("DataStoreService"):GetDataStore("DailyRewards")
local Duration = 24 -- Hours
local RewardsTable = {20, 20, 20, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 75, 75, 75, 100, 100, 200, 1000}

game.Players.PlayerAdded:Connect(function(Player)
	
	local CurrentTime = os.time()
	
	local Data
	
	pcall(function()
		Data = DataStore:GetAsync(Player.UserId.."-DailyReward")
		print("Obtaining data...")
	end)	
	
	if Data ~= nil then
		print("Existing player")
		
		local TimeSinceLastClaim = CurrentTime - Data -- In seconds
		print("Time since last claim "..TimeSinceLastClaim)
		if (TimeSinceLastClaim / 3600) >= Duration then
			print("Player is able to claim Daily Reward")
			local Reward = RewardsTable[math.random(1, #RewardsTable)]
			game.ReplicatedStorage.PresentDailyReward:FireClient(Player, Duration, Reward)
			
			local Connection 
			Connection = game.ReplicatedStorage.ClaimDailyReward.OnServerEvent:Connect(function(RequestingPlayer)
				if RequestingPlayer == Player then
					print("Reward Claimed!")
					Player.Credits.Value = Player.Credits.Value + Reward
					DataStore:SetAsync(Player.UserId.."-DailyReward", os.time())
					Connection:Disconnect()
				end 
			end)
		else
			print("Player is uneligible to receive Reward")
		end
	else
		print("New player found")
		local Reward = RewardsTable[math.random(1, #RewardsTable)]
		game.ReplicatedStorage.PresentDailyReward:FireClient(Player, Duration, Reward)
		local Connection 
		Connection = game.ReplicatedStorage.ClaimDailyReward.OnServerEvent:Connect(function(RequestingPlayer)
			if RequestingPlayer == Player then
				print("Reward Claimed!")
				Player.Credits.Value = Player.Credits.Value + Reward
				DataStore:SetAsync(Player.UserId.."-DailyReward", os.time())
				Connection:Disconnect()
			end 
		end)
	end
end)

Local

local ShopGUI = script.Parent
local DailyRewardScreen = ShopGUI.Shop.Home.DailyReward
local CollectButton = DailyRewardScreen.Collect
local Time = DailyRewardScreen.Time

game.ReplicatedStorage.PresentDailyReward.OnClientEvent:Connect(function(Duration, Reward)

	Time.Text = "Next reward in "..Duration.." hours"
	
	CollectButton.MouseButton1Click:Connect(function()
		game.ReplicatedStorage.ClaimDailyReward:FireServer()
		Time.Text = "Awarded "..Reward.." credits"
	end)
end)
1 Like

Simply, PresentDailyReward:FireClient doesn’t fire, unless it is ready to be claimed! You can fix this by:

Local script:

local ShopGUI = script.Parent
local DailyRewardScreen = ShopGUI.Shop.Home.DailyReward
local CollectButton = DailyRewardScreen.Collect
local Time = DailyRewardScreen.Time

game.ReplicatedStorage.PresentDailyReward.OnClientEvent:Connect(function(Duration, Reward, CanBeClaimed, Timeleft)
local RoundedTime = math.round((Timeleft / 60) /60)
if CanBeClaimed == true then
Time.Text = "Daily Reward Available!" --Optional, Remove if wanted :)
	CollectButton.MouseButton1Click:Connect(function()
		game.ReplicatedStorage.ClaimDailyReward:FireServer()
		Time.Text = "Awarded "..Reward.." credits"
	end)
 
else
Time.Text = "Next reward in "..Duration - RoundedTime.." hours"

end

end)

Server Script (Because its so long, I am just give a couple lines before the edits, and break it up):

print("Player is able to claim Daily Reward")
			local Reward = RewardsTable[math.random(1, #RewardsTable)]
			game.ReplicatedStorage.PresentDailyReward:FireClient(Player, Duration, Reward, true, TimeSinceLastClaim)

and

else
			print("Player is uneligible to receive Reward")
game.ReplicatedStorage.PresentDailyReward:FireClient(Player, Duration, RewardsTable[math.random(1, #RewardsTable)], false, TimeSinceLastClaim)
		end

Let me know if there is errors, or have questions! This script was quite long, and simply issues might be common, as I myself have not tested this

1 Like

Works great so far! Thanks for your reply :grin:

I’ll check back with you in ~23 hours in case there are some problems, but if there are not any, I will mark your reply as the solution :+1:

Thanks again!

1 Like

Just started a test server without any intention to test the daily reward system and I notice that there’s an error in the local script when it comes to new players (i.e., players who have never played the game before). This means that there is no epoch time that a new player’s data can refer to, leading to the daily reward system not working.

Yup, I missed this remote event. Just change it to:

game.ReplicatedStorage.PresentDailyReward:FireClient(Player, Duration, RewardsTable[math.random(1, #RewardsTable)], true, 0)

Edit: Changed the remote event. Now it’s up to date, make sure its set to true, not false

1 Like

I’m prompted with this message:

3rd time is the charm?

game.ReplicatedStorage.PresentDailyReward:FireClient(Player, Duration, Reward, true , 0)

I see that you had a Reward variable so I will try using that instead of math.random. Although we will see if that works, it might output the same thing

1 Like

If that fixed it, then you will need to also change this in your server script:

else
			print("Player is uneligible to receive Reward")

			local Reward = RewardsTable[math.random(1, #RewardsTable)]
			game.ReplicatedStorage.PresentDailyReward:FireClient(Player, Duration, Reward, false, TimeSinceLastClaim)

Unfortunately, it’s the same error.

Not sure if you know, but this message is referring to the local script. Specifically, this line:

game.ReplicatedStorage.PresentDailyReward.OnClientEvent:Connect(function(Duration, Reward, CanBeClaimed, TimeLeft)
	local RoundedTime = math.random((TimeLeft / 60) / 60) -- this one here
	-- so on and so forth

Oh? It’s not math.random, it should be math.round

1 Like

Cool, that seemed to solve the issue!

One last query: what would you recommend to ensure that when the player is prompted their reward (i.e., from the text), the text transitions from this statement to one of “time remaining”?

Thank you for your continuous support :grin:

It might be nice to have a sound for claiming a reward and a text color change. Then use task.wait() to wait for like 5-10 seconds, then change the text color back to normal and change the text back to time remaining. That is just my recommendation

1 Like

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