local LastLogin = RewardDataStore:GetAsync(player.UserId)
local Streak = LastLogin.Streak or 0 -- error
if LastLogin then
local Seconds = os.time() - LastLogin
local Minutes = Seconds / 60
local Hours = Minutes / 60
local StreakAwards = {50, 100, 250, 500, 1000}
local Streak = LastLogin.Streak
Streak = Streak + 1
local Award = StreakAwards[Streak]
if not Award then
Award = StreakAwards[#StreakAwards]
end
UpdateCash(player, Award)
DailyReward:FireClient(player, Streak, Award)
local Data = {}
Data.Streak = Streak
Data.LastLogin = LastLogin
RewardDataStore:SetAsync(player.UserId, Data)
end
local LastLogin = RewardDataStore:GetAsync(player.UserId)
if LastLogin then
local Seconds = os.time() - LastLogin
local Minutes = Seconds / 60
local Hours = Minutes / 60
local StreakAwards = {50, 100, 250, 500, 1000}
local Streak = LastLogin.Streak
Streak = Streak + 1
local Award = StreakAwards[Streak]
if not Award then
Award = StreakAwards[#StreakAwards]
end
UpdateCash(player, Award)
DailyReward:FireClient(player, Streak, Award)
local Data = {}
Data.Streak = Streak
Data.LastLogin = LastLogin
RewardDataStore:SetAsync(player.UserId, Data)
else
local StreakAwards = {50, 100, 250, 500, 1000}
local Streak = LastLogin.Streak -- ERROR
Streak = Streak + 1
local Award = StreakAwards[Streak]
if not Award then
Award = StreakAwards[#StreakAwards]
end
UpdateCash(player, Award)
DailyReward:FireClient(player, Streak, Award)
local Data = {}
Data.Streak = Streak
Data.LastLogin = LastLogin
RewardDataStore:SetAsync(player.UserId, Data)
end
[ attempt to index local ‘LastLogin’ (a nil value)]
local LastLogin = RewardDataStore:GetAsync(player.UserId)
if LastLogin then
local Seconds = os.time() - LastLogin -- ERROR
local Minutes = Seconds / 60
local Hours = Minutes / 60
local Streak = LastLogin.Streak
Streak = Streak + 1
local Award = StreakAwards[Streak]
if not Award then
Award = StreakAwards[#StreakAwards]
end
UpdateCash(player, Award)
DailyReward:FireClient(player, Streak, Award)
local Data = {}
Data.Streak = Streak
Data.LastLogin = LastLogin
RewardDataStore:SetAsync(player.UserId, Data)
else
local Streak = 1
local Award = StreakAwards[Streak]
if not Award then
Award = StreakAwards[#StreakAwards]
end
UpdateCash(player, Award)
DailyReward:FireClient(player, Streak, Award)
local Data = {}
Data.Streak = Streak
Data.LastLogin = LastLogin
RewardDataStore:SetAsync(player.UserId, Data)
end
[ attempt to perform arithmetic on local ‘LastLogin’ (a table value)]
Being that LastLogin is nil, setting Data.LastLogin to LastLogin makes it nil. You should set it to os.time(). That, and you have to do LastLogin.LastLogin to get the time.
local LastLogin = RewardDataStore:GetAsync(player.UserId)
if LastLogin then
local Seconds = os.time() - LastLogin -- ERROR
local Minutes = Seconds / 60
local Hours = Minutes / 60
local Streak = LastLogin.Streak
Streak = Streak + 1
local Award = StreakAwards[Streak]
if not Award then
Award = StreakAwards[#StreakAwards]
end
UpdateCash(player, Award)
DailyReward:FireClient(player, Streak, Award)
local Data = {}
Data.Streak = Streak
Data.LastLogin = LastLogin
RewardDataStore:SetAsync(player.UserId, Data)
else
local Streak = 1
local Award = StreakAwards[Streak]
if not Award then
Award = StreakAwards[#StreakAwards]
end
UpdateCash(player, Award)
DailyReward:FireClient(player, Streak, Award)
local Data = {}
Data.Streak = Streak
Data.LastLogin = os.time()
RewardDataStore:SetAsync(player.UserId, Data)
end
[attempt to perform arithmetic on local ‘LastLogin’ (a table value)]
Ok, now I can just join multiple times and get the rewards
local LoginData = RewardDataStore:GetAsync(player.UserId)
if LoginData then
local Seconds = os.time() - LoginData.LastLogin -- ERROR
local Minutes = Seconds / 60
local Hours = Minutes / 60
local Streak = LoginData.Streak
Streak = Streak + 1
local Award = StreakAwards[Streak]
if not Award then
Award = StreakAwards[#StreakAwards]
end
UpdateCash(player, Award)
DailyReward:FireClient(player, Streak, Award)
local Data = {}
Data.Streak = Streak
Data.LastLogin = os.time()
RewardDataStore:SetAsync(player.UserId, Data)
else
local Streak = 1
local Award = StreakAwards[Streak]
if not Award then
Award = StreakAwards[#StreakAwards]
end
UpdateCash(player, Award)
DailyReward:FireClient(player, Streak, Award)
local Data = {}
Data.Streak = Streak
Data.LastLogin = os.time()
RewardDataStore:SetAsync(player.UserId, Data)
end
So I join once, get reward one, join again get reward 2, etc. It’s not waiting a day
local DataStoreService = game:GetService('DataStoreService')
local RewardDataStore = DataStoreService:GetDataStore('RewardsDataStore', '001')
game.Players.PlayerAdded:connect(function(Player)
local RewardData = RewardDataStore:GetAsync(tostring(Player.UserId))
if typeof(RewardData) == "table" then
local LastLogin = RewardData[1]
local DaysInRow = RewardData[2]
local Seconds = os.time() - LastLogin
local Minutes = Seconds / 60
local Hours = Minutes / 60
if Hours >= 12 and Hours <= 36 then -- they logged in 12 to 36 hours since their last (change as you desire)
RewardDataStore:SetAsync(tostring(Player.UserId), {os.time(), DaysInRow + 1})
UpdateCash(Player, 50)
DailyReward:FireClient(Player)
end
else
RewardDataStore:SetAsync(tostring(Player.UserId), {os.time(), 0})
UpdateCash(Player, 50)
DailyReward:FireClient(Player)
end
end)
This works. You need to check between X and Y hours. And it tracks how many days in a row, too!
…? This is being done on the server though. tick() returns the number of seconds since the UNIX Epoch in the server’s time zone, os.time() returns the number of seconds since the UNIX Epoch in UTC explicitly.
Why would you use tick over os.time? This is especially so when it’s on the server. If it’s on the client, then be it tick or os.time, the client can change what it returns easily.
Use os.time() on server because different servers can be in different timezones - tick() is based on the local time of the machine that is running the script.
You could add a Streak variable. Saving that along with LastLogin.
Each day add 1 to Streak then save. after 5 days reset it to 1 again just after the player receives their 5th day reward.
Rewards can be in a table like:
StreakRewards = {10, 20, 30, 40, 50}
using their streak to identify what index the day is like:
Streak = 3
print(StreakRewards[Streak]) – prints 30
use that to add to whatever.