Run code even if the player isn't playing

Hi! I’ve seen lots of games where even when you aren’t playing them you’re still winning money and when you come back you have that money, I think the same method would be used for a daily reward system but I don’t know how they make it, does anyone know how to do it?

If there is not check for a player in your script then your script doesn’t need the player to run. Also if there is no player in a roblox server but the server is still running, the code will as well.
Lastly if there are no players in a game but they have outstanding “rewards” to claim daily just use os.time() to determine if it’s true or not.

Here’s a daily reward system, Sadly I forgot who it was from so I can’t credit them :frowning: :

local dr = {}

local Players = game:GetService("Players")
local dataService = game:GetService("DataStoreService")
local timeData = dataService:GetDataStore(script:GetAttribute("ServiceName"))
local dayDifference = 1

function dr.Joined(plr)
	local timeKey = (plr.UserId..'-$loginTime')
	
	local startingDate
	local claimedToday = Instance.new("BoolValue")
	
	claimedToday.Parent = plr -- this will be a child of the player 
	claimedToday.Name = "claimedToday" -- [name of the item](inside the plr)
	
	local success, err = pcall(function()
		
		startingDate = timeData:GetAsync(timeKey)
	end)
	
	if success then
		
		if startingDate then
			
			local startingString = string.split(startingDate,' ')
			local todayDate = os.date()
			local splitString = string.split(todayDate,' ')
			
			if tonumber(splitString[3]) == tonumber(startingString[3] + dayDifference) and splitString[2] == startingString[2] and splitString[1] ~= startingString[1] then
				
				print('Day after!')
				claimedToday.Value = true
				
			elseif splitString[1] == startingString[1] then
				print('Same Day - '..os.date("%A"))
				
			else
				
				print(tonumber(splitString[3] - startingString[3]), 'days missed!')
			end
			
		else
			
			print('No past date!')
		end
		
	else
		
		print(err)
	end
end

function dr.Leaving(plr)
	
	if plr:WaitForChild("claimedToday").Value == true then
		
		local timeKey = (plr.UserId..'-$loginTime')
		
		local success, err = pcall(function()
			
			timeData:SetAsync(timeKey,os.date())
		end)
		
		if success then
			
			print(plr.Name..' left and data saved successfully!')
		else
			
			error(err)
		end
		
	end
end

-- just call the function like this [[ dr:Joined(Player) ]]
return dr

To explain things, they don’t actually run code when the player isn’t playing, rather when the player leaves they save a lastPlayed time in a data store, when they rejoin they see how much time has elapsed since the lastPlayed time.

Running code for a player who isn’t online is not really an option. The best way to do this is to calculate this when the player rejoins.