My custom daily reward script doesnt work properly (reupload cause no one saw it)

So I made my custom daily reward script and I have 2 issues with it (issues listed below script)

local DataStoreService = game:GetService("DataStoreService")

local DailyRewardInfoDS = DataStoreService:GetDataStore("DailyRewardInfoDS")

local ChooseDailyRewardVerdict = game.ReplicatedStorage.RemoteEvents.ChooseDailyRewardVerdict

local GiveDailyReward = game.ReplicatedStorage.RemoteEvents.GiveDailyReward

local StreakRewards = {
	[1] = {250};
	[2] = {500};
	[3] = {750};
	[4] = {1000};
	[5] = {2500};
	[6] = {5000};
	[7] = {6500,60};
}

game.Players.PlayerAdded:Connect(function(player)
	
	player.Chatted:Connect(function(msg)
		
		if msg == "/dr" or "/dailyreward" then
			
			ChooseDailyRewardVerdict:FireClient(player)
			
		end
		
	end)
	
	GiveDailyReward.OnServerEvent:Connect(function(player)
		
		if tick() < (player.DailyRewardInfo.ClaimTime.Value + 86400) then
			
			player.DailyRewardInfo.DailyRewardStreak.Value += 1
			
			if StreakRewards[player.DailyRewardInfo.DailyRewardStreak.Value][2] then
				
				player.leaderstats.Coins.Value += StreakRewards[player.DailyRewardInfo.DailyRewardStreak.Value][1]
				
				player.leaderstats.Gems.Value += StreakRewards[player.DailyRewardInfo.DailyRewardStreak.Value][2]
				
				player.DailyRewardInfo.ClaimTime.Value = tick() + 86400
				
			else
				
				player.leaderstats.Coins.Value += StreakRewards[player.DailyRewardInfo.DailyRewardStreak.Value][1]
				
				player.DailyRewardInfo.ClaimTime.Value = tick() + 86400
				
			end
			
		else
			
			player.DailyRewardInfo.DailyRewardStreak.Value = 1

			player.leaderstats.Coins.Value += StreakRewards[player.DailyRewardInfo.DailyRewardStreak.Value][1]
			
			player.DailyRewardInfo.ClaimTime.Value = tick() + 86400
			
		end
		
	end)

	local dailyrewardinfo = Instance.new("Folder")
	dailyrewardinfo.Name = "DailyRewardInfo"
	dailyrewardinfo.Parent = player

	local claimtime = Instance.new("IntValue")
	claimtime.Name = "ClaimTime"
	claimtime.Parent = dailyrewardinfo

	local allowedtoclaim = Instance.new("BoolValue")
	allowedtoclaim.Name = "AllowedToClaim"
	allowedtoclaim.Parent = dailyrewardinfo
	
	local dailyrewardstreak = Instance.new("IntValue")
	dailyrewardstreak.Name = "DailyRewardStreak"
	dailyrewardstreak.Parent = dailyrewardinfo

	local data

	local success, errorMessage = pcall(function()

		data = DailyRewardInfoDS:GetAsync("DailyRewardInfo-" .. player.UserId)

	end)

	if success then

		if data then

			claimtime.Value = data.ClaimTime
			allowedtoclaim.Value = data.AllowedToClaim
			dailyrewardstreak.Value = data.DailyRewardStreak

			print("Successfully loaded " .. player.Name .. "'s DailyRewardInfo data!")

		else

			claimtime.Value = 0
			allowedtoclaim.Value = true
			dailyrewardstreak.Value = 0

		end

	else

		warn(errorMessage)

	end

end)

game.Players.PlayerRemoving:Connect(function(player)

	local data = {
		ClaimTime = player.DailyRewardInfo.ClaimTime.Value;
		AllowedToClaim = player.DailyRewardInfo.AllowedToClaim.Value;
		DailyRewardStreak = player.DailyRewardInfo.DailyRewardStreak.Value;
	}

	local success, errorMessage = pcall(function()

		DailyRewardInfoDS:SetAsync("DailyRewardInfo-" .. player.UserId, data)

	end)

	if success then

		print("Successfully saved " .. player.Name .. "'s DailyRewardInfo data!")

	else

		warn(errorMessage)

	end

end)

game:BindToClose(function()

	for _, player in pairs(game.Players:GetPlayers()) do

		player:Kick()

	end

	wait(3)

end)

Issue #1: When I chat “dr” or “daily” it still prompts the person with the daily reward (even without the /)

Issue #2: It doesn’t give the player the correct streakreward

First things first this is the cause of issue #1, change that to this:
if msg == "/dr" or msg == "/dailyreward" then

Alr but now issue #2 how do I fix that

Before that add print(player.DailyRewardInfo.DailyRewardStreak.Value) and tell me what it prints

it prints the num 2 and num 2 is 500 cash and i had previously 3,750 but it gave me only up to 4k but i was supposed to get 4,250

Ok, now above that print(StreakRewards[player.DailyRewardInfo.DailyRewardStreak.Value][1]

it prints 250 for streak 1 as if everytime the player claims the dailt reward it resets the streak to 1 because i made a line of code that detects if the player hasnt claimed their reward in 2 days or more then it resets their streak to 1

  • You’re binding the GiveDailyReward event in Players.PlayerAdded, but the event already fires for every player. This means that the event will fire more than once when you introduce more players. You should just bind the event outside of any other ones in top-level scope.
GiveDailyReward.OnServerEvent:Connect(function(player)
	-- daily reward logic
end)

game.Players.PlayerAdded:Connect(function(player)
	-- player.Chatted event
	-- DailyRewardInfo setup
end)
  • Looking at tick() documentation:

This could mess the script up in the future because the server’s time could be in a different timezone, which means a different Unix epoch. os.time() returns the number of seconds in the UTC timezone, so you could use that instead.

  • There’s no logic for when the player tries to get a daily reward earlier than 24 hours. You could just check tick() < claimTime.Value and return if so.

  • I think this reads better for the daily reward logic.

GiveDailyReward.OnServerEvent:Connect(function(player)
	local claimTime = player.DailyRewardInfo.ClaimTime
	local streak = player.DailyRewardInfo.DailyRewardStreak
	
	if os.time() < claimTime.Value then
		return
	elseif os.time() < claimTime.Value + 86400 then
		-- this prevents errors after the 7th day
		streak.Value = math.min(streak.Value + 1, #StreakRewards)
	else
		streak.Value = 1
	end

	local reward = StreakRewards[streak.Value]
	player.leaderstats.Coins.Value += reward[1]
	player.leaderstats.Gems.Value += reward[2] or 0

	claimTime.Value = os.time() + 86400
end)

I’m not sure if any of this will fix your issue, but it might fix future issues.

What does this mean? Since ur using the streakrewards for the streak value

I’m using the length (hence #) of the StreakRewards table to cap the streak’s value to the maximum reward, which is the 7th one, {6500, 60}. If you add another reward to the table, say for day 8, #StreakRewards will become 8 too.

Why would use 1, #streakrewards when I will never increase the streak by 2 because it’s 1 per day

If you were to load a player’s data from yesterday, wouldn’t DailyRewardStreak stay increased?

  1. Fresh new data
    DailyRewardStreak.Value = 0

  2. Player’s ClaimTime is super low (0), so DailyRewardStreak would be reset
    DailyRewardStreak.Value = 1

  3. Give them StreakRewards[1]

  4. Player leaves, DailyRewardStreak has been saved (!)

  5. Player comes back tomorrow…
    DailyRewardStreak is loaded back in (!)

  6. Player’s ClaimTime is just right! Increase the streak by 1
    DailyRewardStreak.Value += 1

  7. Now the player’s streak is 2, so they should get StreakRewards[2]

  8. Player leaves, DailyRewardStreak has been saved (!)

  9. Player comes back tomorrow… same as yesterday

Eventually, you could get to an 8 day streak, which would make DailyRewardStreak's value 8. StreakRewards[8] == nil, so it would error when you check whether there is a gems reward.

Are there any mistakes in my logic? Do they have a fake daily reward streak?

I want the max reward to be 7 days and then it resets

1 Like

In that case, you could use % to loop your reward back to the first day.

elseif claimTime.Value < os.time() + 86400 then
	streak.Value = streak.Value % #StreakRewards + 1