so i have a rewards system using datstore but seems like i get every time i join i wanna make it daily but its very weird
here is the code
--// Created by SystemFormat on 6/2/2018
local TimePeriod = 200
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local DataStore = DataStoreService:GetDataStore("RewardSystem")
local days = DataStoreService:GetDataStore("DaysStore")
local function RewardPlayer(Player,days)
print(days)
local Success, Message = pcall(function()
print("Eligible - "..Player.Name)
end)
game.ReplicatedStorage.RewardSystem.ShowGui:FireClient(Player,days)
end
Players.PlayerAdded:Connect(function(Player)
local daysval = Instance.new("IntValue")
daysval.Value = 1
daysval.Name = "Days"
daysval.Parent = Player
local daysdata
local s,r = pcall(function()
daysdata = days:GetAsync(Player.UserId) or 1
end)
if s then
print("have data")
daysval.Value = daysdata
else
print("doesnt")
daysval.Value = 1
end
local CurrentTime = os.time()
local SaveRetrieved = DataStore:GetAsync(Player.UserId)
if SaveRetrieved then
if type(SaveRetrieved) == "number" then
local ElapsedTime = CurrentTime - SaveRetrieved
if ElapsedTime >= TimePeriod then
print(ElapsedTime)
RewardPlayer(Player,daysval.Value)
else
print("Ineligible")
end
else
print("Corrupted")
DataStore:SetAsync(Player.UserId, os.time())
days:SetAsync(Player.UserId,daysval.Value)
end
else
print("Creating")
DataStore:SetAsync(Player.UserId, os.time())
days:SetAsync(Player.UserId,daysval.Value)
RewardPlayer(Player,daysval.Value)
end
end)
game.ReplicatedStorage.RewardSystem.GiveMoney.OnServerEvent:Connect(function(plr,cash)
plr.leaderstats.Cash.Value += cash
plr.Days.Value += 1
days:SetAsync(plr.UserId,plr.Days.Value)
end)
oh hi there wasnt any errors tho its just that im getting the reward every time i join and not every 24 hours and sometimes the days value skips but there isnt any single error
Alright. Here is a script that I made for my game.
game.Players.PlayerAdded:Connect(function(Player)
local DataStoreService = game:GetService("DataStoreService"):GetDataStore("DailyRewardService") -- Define the Datastore service and define our datastore name
local LastLogin -- Define a variable that we will use later.
pcall(function()
LastLogin = DataStoreService:GetAsync(Player.UserId) -- Check if the player joined already in the past 24 hours
end)
if LastLogin and (os.time() - LastLogin >= 86400) then
-- os.time() returns how many seconds have passed since the Unix epoch (1 January 1970, 00:00:00)
-- 86400 is the number of seconds in one day
DataStoreService:SetAsync(Player.UserId, os.time()) -- We update the player datastoreto the current Unix epoch
[YOUR_EVENT]:FireClient(Player, YOUR_ARGUMENTS) -- We fire an event ()
Player.leaderstats.Coins.Value += 100 -- Add stats to the user (feel free to change that to whatever you want)
elseif not LastLogin then -- If the player never joined the game before
DataStoreService:SetAsync(Player.UserId, os.time()) -- We update the player datastoreto the current Unix epoch
[YOUR_EVENT]:FireClient(Player, YOUR_ARGUMENTS) -- We fire an event ()
Player.leaderstats.Coins.Value += 100 -- Add stats to the user (feel free to change that to whatever you want)
else
-- Else, the player already joined in the past 24 hours
[YOUR_EVENT]:FireClient(Player, YOUR_ARGUMENTS)
end
end)
Also, it is important to change the stats of a player server-sided. Else, exploiters could just run a RemoteEvent again and again to receive infinite stats.
ok i tried you script but it isnt what i want my system needs days where each days the rewards goes more and more but yours doesnt also just a reminder when i wrote my script it worked fine but after i implemented days system it broke
game.Players.PlayerAdded:Connect(function(Player)
local DataStoreService = game:GetService("DataStoreService"):GetDataStore("DailyRewardService") -- Define the Datastore service and define our datastore name
local LastLogin -- Define a variable that we will use later.
pcall(function()
LastLogin = DataStoreService:GetAsync(Player.UserId) -- Check if the player joined already in the past 24 hours
end)
if LastLogin and (os.time() - LastLogin.Unix >= 86400) then
-- os.time() returns how many seconds have passed since the Unix epoch (1 January 1970, 00:00:00)
-- 86400 is the number of seconds in one day
DataStoreService:SetAsync(Player.UserId, {
Unix = os.time(),
Days = LastLogin.Days + 1
}) -- We update the player datastoreto the current Unix epoch
[YOUR_EVENT]:FireClient(Player, YOUR_ARGUMENTS) -- We fire an event ()
elseif not LastLogin then -- If the player never joined the game before
DataStoreService:SetAsync(Player.UserId, {
Unix = os.time(),
Days = 1
}) -- We update the player datastoreto the current Unix epoch
[YOUR_EVENT]:FireClient(Player, YOUR_ARGUMENTS) -- We fire an event ()
else
-- Else, the player already joined in the past 24 hours
[YOUR_EVENT]:FireClient(Player, YOUR_ARGUMENTS)
end
end)
ServerScriptService.DailyReward:13: attempt to perform arithmetic (sub) on number and nil
do you know that error?
if you want the local script its here
local rewardsdata = require(game.ReplicatedStorage.RewardConfig)
local cash
game.ReplicatedStorage.RewardSystem.ShowGui.OnClientEvent:Connect(function(days)
print(days)
print(typeof(days))
for i,v in pairs(script.Parent:GetChildren()) do
if v:IsA("Frame") then
for di = 1, days, 1 do
if v.Name == "Reward"..di then
v.BackgroundColor3 = Color3.fromRGB(43, 192, 255)
else
v.BackgroundColor3 = Color3.fromRGB(86,86,86)
end
end
end
end
for i,v in pairs(rewardsdata.rewards) do
if tostring(days) == i then
cash = v
end
end
script.Parent.Parent:TweenPosition(UDim2.new(0.5,0,0.5,0), "Out", "Bounce",1 )
end)
script.Parent.Parent.Cashout.MouseButton1Click:Connect(function()
game.ReplicatedStorage.RewardSystem.GiveMoney:FireServer(cash)
script.Parent.Parent:TweenPosition(UDim2.new(3,0,0.5,0), "Out", "Bounce",1 )
end)