Why is reward nil RIGHT after I defined it?

I am making a daily reward system and this is the code I have:

The Code

local DataStore2 = require(1936396537)

local DataStore = game:GetService(“DataStoreService”):GetDataStore(“DailyRewards”)

local hourWait = 12

local possibleRewards = {100, 100,100, 1000, 1000, 1000, 1000, 1000, 10000, 10000, 10000, 100000, 100, 100}

game.Players.PlayerAdded:Connect(function(player)
local timeNow = os.time()

local data

pcall(function()
	data = DataStore:GetAsync(player.UserId.."-dailyReward")
	print("GettingData...")
end)

if data ~= nil then
	local timeSinceLastClaim = timeNow - data
	print("Time since last claim is "..timeSinceLastClaim)
	
	if (timeSinceLastClaim / 3600) >= hourWait then
		local reward = possibleRewards[math.rad(1,#possibleRewards)]
		print("CODE55 "..reward)
		game.ReplicatedStorage.DailyRewardEvent:FireClient(player, hourWait, reward)
		local connection
		connection = game.ReplicatedStorage.DailyRewardEvent.OnServerEvent:Connect(function(triggeringPlayer)
			if triggeringPlayer == player then
				print("Reward Claimed!")
				local coinsStore = DataStore2("coins", player)
				coinsStore:Increment(reward)
				DataStore:SetAsync(player.UserId.."-dailyReward", os.time())
				connection:Disconnect()
			end
		end)
	else
		print("Player is uneligible right now")
	end
else
	warn("Data ain't nil!")
	local reward = possibleRewards[math.rad(1,#possibleRewards)]
	print(reward)
	game.ReplicatedStorage.DailyRewardEvent:FireClient(player, hourWait, reward)
	local connection
	connection = game.ReplicatedStorage.DailyRewardEvent.OnServerEvent:Connect(function(triggeringPlayer)
		if reward == player then
			print("Reward Claimed!")
			local coinsStore = DataStore2("coins", player)
			coinsStore:Increment(reward)
			DataStore:SetAsync(player.UserId.."-dailyReward", os.time())
			connection:Disconnect()
		end
	end)
end

end)

--[[game.Players.PlayerRemoving:Connect(function(player)
local data
pcall(function()
	data = DataStore:GetAsync(player.UserId.."-dailyReward")
	print("Getting Data...")
end)

if data == nil then
	-- New player
	pcall(function()
		local timeVal = os.time()
		DataStore:SetAsync(player.UserId.."-dailyReward")
	end)
	print("Saved because you are a new player")
end

end) --]]

It printed “Data ain’t nil” and right after it says the reward is nil. It is super confusing. Please help!

warn("Data ain't nil!")
local reward = possibleRewards[math.rad(1,#possibleRewards)]
print(reward)
3 Likes

I think you mean math.random? math.rad converts the number to radians.

2 Likes

reward is the variable for how much I want to give them in this case I am randomly getting an index number for the possibleRewards table. Which will determine the reward.

local reward = possibleRewards[math.random(1,#possibleRewards)]

Use math.random instead of math.rad

2 Likes

OHH LOL my bad ty for the help