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