How can my Daily Rewards script improve?

Hey! How is it going? A while back, I created a Daily Rewards system. The thing is, it uses a while loop, and the script’s name is the player’s name. Can this get more reliable? Thanks! WE

local player = game.Players:FindFirstChild(script.Name)
local DataStore2 = require(game:GetService("ServerScriptService"):WaitForChild("DataStore2"))
local Datastore = DataStore2.Combine("DailyRewards", "DailyRewardsBackup")
local config = require(script.Parent.Parent.MainConfig)
local cansave = true
local dnew = false

game.ReplicatedStorage.GiveMoney.OnServerEvent:Connect(function(plr)
	if player == plr then
		if dnew == false then
			dnew = true
		end
	end
end)

while true do
	game:GetService("RunService").Heartbeat:Wait()
	if player then
		local datastore = DataStore2("DailyRewards", player)
		local timelast
		local success,output = pcall(function()
			timelast = datastore:Get(os.time())
		end)
		if timelast then
			local done = false
			local reset = false	
			repeat
				game:GetService("RunService").Heartbeat:Wait()
				if (os.time() - timelast)/3600 >= 48 then
					done = true
					reset = true
				elseif (os.time() - timelast)/3600 >= 24 then
					done = true
				end
			until done == true
			local daysfolder = player:FindFirstChild("Days")
			local max = 0
			for i,v in pairs(daysfolder:GetChildren()) do
				local now = tonumber(v.Name)
				if now > max then
					max = now
				end
			end
			if reset then
				max = 0
				daysfolder:ClearAllChildren()
			else
				local nextnum = max+1
				if nextnum-1 == config.Days then
					daysfolder:ClearAllChildren()
					nextnum = 1
				end

				local reward, ownvip = config.checkreward(nextnum, player)
				dnew = false
				game.ReplicatedStorage.ShowGui:FireClient(player, nextnum, ownvip)
				repeat 
					game:GetService("RunService").Heartbeat:Wait()
				until dnew == true
				local bool = Instance.new("BoolValue")
				bool.Name = nextnum
				bool.Parent = daysfolder
				player.leaderstats[config.CurrencyName].Value += reward

			end
			local success, output = pcall(function()
				datastore:Set(os.time())
			end)

			if not success then
				error(output)
			end
		else
			if cansave then
				cansave = false
				local success, output = pcall(function()
					datastore:Set(os.time())
				end)
				if not success then
					error(output)
				end
			end
		end
	end
end

Is there a reason you would put

game:GetService(“RunService”).Heartbeat:Wait()

into a while loop?

You really shouldn’t use wait().

You could just connect the event to a function.

game:GetService(“RunService”).Heartbeat:Connect(function()

end)

1 Like

Also, regarding the part about naming the script the player’s name, you can remove the variable and put the loop thing into a playeradded event, which provides player as a variable

1 Like

I will just link my reply down below. Please don’t tell other people to what to use or what to not when you yourself aren’t even well versed with this in the first place, as you’re going to teach many people bad practices.

1 Like

In this scenario it doesn’t really matter, pretty sure avoiding wait is when you need precision, if we’re avoiding wait() you should be avoiding while true loops also, just connect the entire thing to the heartbeat function.