Daily Rewards Issues

I keep getting this error and I am unsure how to fix it.

Error:
Players.max123lover2.PlayerGui.HUD.hud_handler:203: attempt to index nil with 'lastClaim'

Server Script:

local rs = game:GetService("ReplicatedStorage")
local dss = game:GetService("DataStoreService")

local events = rs.Game.Events.RemoteFunctions
local dataStore = dss:GetDataStore("dailyrewardsTest_001")

local Rewards = require(rs.Game.Modules.daily_module)

local default = {
	currentDay = 1,
	lastClaim = 0
}

local Hour24 = 60*60*24
local Hour48 = Hour24*2

local function getDayIndex(player)
	local lastReward = dataStore[default]

	local currentTime = workspace:GetServerTimeNow()
	local timeDifference = currentTime - lastReward.lastClaim

	local dayIndex = lastReward.currentDay

	if timeDifference >= Hour48 then
		dayIndex = 1
	end

	return dayIndex
end

local function claim(player)
	local lastReward = dataStore[default]
	
	local currentTime = workspace:GetServerTimeNow()
	local timeDifference = currentTime - lastReward.lastClaim

	if timeDifference < Hour24 then
		print("Has not been 24 hours")
		return false
	end

	local DayReward = getDayIndex(player)
	local amountOfCash = Rewards[DayReward]
	
	player.savingFolder[amountOfCash.Reward.Currency].Value += amountOfCash.Reward.Amount

	local newDayIndex = DayReward + 1

	if newDayIndex > #Rewards then
		newDayIndex = 1
	end
	
	default.currentDay = newDayIndex
	default.lastClaim = currentTime
	
	dataStore:SetAsync(default.currentDay)
	dataStore:SetAsync(default.lastClaim)
end

events.Claim.OnServerInvoke = function(player)
	claim(player)
	
	local lastReward = dataStore[default]

	return lastReward
end

events.GetInfo.OnServerInvoke = function(player)
	local lastReward = dataStore[default]

	return lastReward
end```

Client Script:
```lua
local daily_data = require(rs.Game.Modules.daily_module)

local Hour24 = 60*60*24
local Hour48 = Hour24*2

local dailyFrame = Frames:WaitForChild("DailyFrame")
local awards = dailyFrame:WaitForChild("Award")
local claimButton = dailyFrame:WaitForChild("Button")
local closeButton = dailyFrame:WaitForChild("CloseButton")
local timer = dailyFrame:WaitForChild("Timer")

for day, amount in daily_data do
	local dayFrame = awards:FindFirstChild(day)

	dayFrame.Day.Text = "Day ".. daily_data[day].DayNumber
	dayFrame.Reward.Text = daily_data[day].Reward.Amount.." ".. daily_data[day].Reward.Currency
end

local info = events.RemoteFunctions.GetInfo:InvokeServer() -- Where it errors

local function numberToTime(number)
	local hours = math.floor(number / 60 / 60)
	local minutes = math.floor(number / 60) % 60
	local seconds = math.floor(number % 60)

	if minutes < 10 then
		minutes = "0".. minutes
	end

	if seconds < 10 then
		seconds = "0".. seconds
	end

	return hours ..":".. minutes..":".. seconds
end

local function updateUi()
	local currentTime = workspace:GetServerTimeNow()
	local lastClaim = table.find(info.lastClaim)
	local timeDifference = currentTime - lastClaim

	claimButton.Visible = timeDifference > Hour24
	timer.Visible = timeDifference <= Hour24

	timer.Text = numberToTime(Hour24 - timeDifference)

	local currentDay = table.find(info.currentDay)

	if timeDifference > Hour48 then
		currentDay = 1
	end

	for _, award in awards:GetChildren() do
		if award:IsA("Frame") then
			local awardDay = tonumber(award.Name)
			local selected = awardDay == currentDay
		
			if selected then
				award.UIStroke.Enabled = true
			else
				award.UIStroke.Enabled = false
			end
		end
	end
end

claimButton.Activated:Connect(function()
	info = events.RemoteFunctions.Claim:InvokeServer()
	updateUi()
end)

closeButton.Activated:Connect(function()
	dailyFrame.Visible = false
end)

dailyFrame.Visible = true

while task.wait(1) do
	updateUi()
end```

Help is appreciated, I understand it is a pretty big script.

The client script is only 80 lines long, but the script the error references to has over 200 lines. Can you send the code in hud_handler?

I haven’t read your code fully so I may be wrong, but I’m assuming from the serverinvoke event that you’re doing this on the client. Using the getservertimenow method on the client throws an error because it has to be called from the server.

It specifies a client that isn’t connected (not sure how you wouldn’t be connected), so it will run fine.

Oh yes, my apologies.

30char

1 Like

I’m so far out on the untested scripting I have little faith in this. But, I put some work into it so …

Server Script
local rs = game:GetService("ReplicatedStorage")
local dss = game:GetService("DataStoreService")

local events = rs.Game.Events.RemoteFunctions
local dataStore = dss:GetDataStore("dailyrewardsTest_001")

local Rewards = require(rs.Game.Modules.daily_module)

local default = {
    currentDay = 1,
    lastClaim = 0
}

local Hour24 = 60 * 60 * 24
local Hour48 = Hour24 * 2

local function getDayIndex(player)
    local lastReward = dataStore:GetAsync(player.UserId) or default
    local currentTime = workspace:GetServerTimeNow()
    local timeDifference = currentTime - lastReward.lastClaim
    local dayIndex = lastReward.currentDay

    if timeDifference >= Hour48 then
        dayIndex = 1
    end

    return dayIndex
end

local function claim(player)
    local lastReward = dataStore:GetAsync(player.UserId) or default
    local currentTime = workspace:GetServerTimeNow()
    local timeDifference = currentTime - lastReward.lastClaim

    if timeDifference < Hour24 then
        return false
    end

    local DayReward = getDayIndex(player)
    local amountOfCash = Rewards[DayReward]
    
    player.savingFolder[amountOfCash.Reward.Currency].Value += amountOfCash.Reward.Amount

    local newDayIndex = DayReward + 1

    if newDayIndex > #Rewards then
        newDayIndex = 1
    end
    
    lastReward.currentDay = newDayIndex
    lastReward.lastClaim = currentTime
    
    dataStore:SetAsync(player.UserId, lastReward)
end

events.Claim.OnServerInvoke = function(player)
    claim(player)
    return dataStore:GetAsync(player.UserId) or default
end

events.GetInfo.OnServerInvoke = function(player)
    return dataStore:GetAsync(player.UserId) or default
end
Client Script
local rs = game:GetService("ReplicatedStorage")
local events = rs.Game.Events.RemoteFunctions

local Hour24 = 60 * 60 * 24
local Hour48 = Hour24 * 2

local dailyFrame = Frames:WaitForChild("DailyFrame")
local awards = dailyFrame:WaitForChild("Award")
local claimButton = dailyFrame:WaitForChild("Button")
local closeButton = dailyFrame:WaitForChild("CloseButton")
local timer = dailyFrame:WaitForChild("Timer")

local info = events.RemoteFunctions.GetInfo:InvokeServer()

local function numberToTime(number)
    local hours = math.floor(number / 3600)
    local minutes = math.floor(number / 60) % 60
    local seconds = math.floor(number % 60)
    minutes = minutes < 10 and "0" .. minutes or minutes
    seconds = seconds < 10 and "0" .. seconds or seconds
    return hours .. ":" .. minutes .. ":" .. seconds
end

local function updateUi()
    if not info or not info.lastClaim or not info.currentDay then
        return
    end

    local currentTime = workspace:GetServerTimeNow()
    local lastClaim = info.lastClaim
    local timeDifference = currentTime - lastClaim

    claimButton.Visible = timeDifference > Hour24
    timer.Visible = timeDifference <= Hour24
    timer.Text = numberToTime(Hour24 - timeDifference)

    local currentDay = info.currentDay

    if timeDifference > Hour48 then
        currentDay = 1
    end

    for _, award in awards:GetChildren() do
        if award:IsA("Frame") then
            local awardDay = tonumber(award.Name)
            award.UIStroke.Enabled = awardDay == currentDay
        end
    end
end

claimButton.Activated:Connect(function()
    info = events.RemoteFunctions.Claim:InvokeServer()
    updateUi()
end)

closeButton.Activated:Connect(function()
    dailyFrame.Visible = false
end)

dailyFrame.Visible = true

while task.wait(1) do
    updateUi()
end

Maybe it would have been better to just tell you my assumptions:
Your error suggests that the info table received from the server does not contain the expected lastClaim field.

This actually works!! Thank you I appreciate it. Just some parts that I need to fix up about the rewards part of it!

2 Likes

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