local DataStoreService = game:GetService("DataStoreService")
local DataStoreFDW = DataStoreService:GetDataStore("FDW")
local FDW
local DataStoreDailyReward = DataStoreService:GetDataStore("DailyReward")
local DailyReward
local DailyReward_1
local DailyReward_2
local RemoteEventFDW = game.ReplicatedStorage.FDW
local DataStoreFirstJoin = DataStoreService:GetDataStore("FirstJoin")
game.Players.PlayerAdded:Connect(function(Player)
pcall(function()
FDW = DataStoreFDW:GetAsync(Player.UserId)
DailyReward = DataStoreDailyReward:GetAsync(Player.UserId)
DailyReward_1 = string.gsub(DailyReward, DailyReward:sub(12,-5), "")
DailyReward_2 = string.gsub(os.date(), os.date():sub(12,-5), "")
if DataStoreFirstJoin:GetAsync(Player.UserId) == nil then
DataStoreFirstJoin:SetAsync(Player.UserId, true)
else
if DataStoreFirstJoin:GetAsync(Player.UserId) == true then
FDW = 0
DataStoreFirstJoin:SetAsync(Player.UserId, false)
if DailyReward_1 ~= DailyReward_2 then
FDW = FDW + 50
DataStoreDailyReward:SetAsync(Player.UserId, os.date())
end
else
if DailyReward_1 ~= DailyReward_2 then
FDW = FDW + 50
DataStoreDailyReward:SetAsync(Player.UserId, os.date())
end
end
end
end)
end)
game.Players.PlayerRemoving:Connect(function(Player)
pcall(function()
DataStoreFDW:SetAsync(Player.UserId, FDW)
end)
end)
while wait() do
RemoteEventFDW:FireAllClients(FDW)
end
Can you give some more information please? What is supposed to happen? Do you get any errors? What is wrong with it (for instance, does it only complete half the task and doesn’t complete the rest)? We can’t really do much if you don’t explain what the issue is. Thanks.
I would always recommend to handle a pcall() just incase something fails.
I tried improving your code and removing some unnecessary parts.
Im not sure if this would work but hopefully it does.
local DataStoreService = game:GetService("DataStoreService")
local DataStoreFDW = DataStoreService:GetDataStore("FDW")
local FDW
local DataStoreDailyReward = DataStoreService:GetDataStore("DailyReward")
local DailyReward
local DailyReward_1
local DailyReward_2
local RemoteEventFDW = game.ReplicatedStorage.FDW
local DataStoreFirstJoin = DataStoreService:GetDataStore("FirstJoin")
local result
game.Players.PlayerAdded:Connect(function(Player)
-- GETTING FDW AND DAILY REWARD DATA --
local success_2,err_2 = pcall(function()
FDW = DataStoreFDW:GetAsync(Player.UserId)
DailyReward = DataStoreDailyReward:GetAsync(Player.UserId)
end)
if success_2 then
DailyReward_1 = string.gsub(DailyReward, DailyReward:sub(12,-5), "")
DailyReward_2 = string.gsub(os.date(), os.date():sub(12,-5), "")
else -- if pcall() fails, warn
warn(err_2)
end
-- CHECKING IF PLAYER FIRST TIME JOINED --
local success,err = pcall(function()
result = DataStoreFirstJoin:GetAsync(301064597)
end)
if success then
if not result then -- I have shortended this part of code, simply because the :SetAsync(Player.UserId, true) was uneccessary and can be unrealiable
FDW = 0
-- Basically if the player first time joined, this code will be executed.
pcall(function()
DataStoreFirstJoin:SetAsync(Player.UserId, false)
end)
if DailyReward_1 ~= DailyReward_2 then
FDW = FDW + 50
DataStoreDailyReward:SetAsync(Player.UserId, os.date())
end
else -- If the player already joined once, execute code below.
if DailyReward_1 ~= DailyReward_2 then
FDW = FDW + 50
DataStoreDailyReward:SetAsync(Player.UserId, os.date())
end
end
else -- if pcall() fails, warn
warn(err)
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local success_3, err_3 = pcall(function()
DataStoreFDW:SetAsync(Player.UserId, FDW)
end)
if success_3 then
print("Successfully saved "..Player.." data!")
else -- if pcall() fails, warn
warn(err_3)
end
end)
while wait() do
RemoteEventFDW:FireAllClients(FDW)
end
I’ve do this but that doesn’t work … I think the problem is when I check if FirstJoin is nil …
local DataStoreService = game:GetService("DataStoreService")
local DataStoreFDW = DataStoreService:GetDataStore("FDW")
local FDW
local DataStoreDailyReward = DataStoreService:GetDataStore("DailyReward")
local DailyReward
local DailyReward_1
local DailyReward_2
local RemoteEventFDW = game.ReplicatedStorage.FDW
local DataStoreFirstJoin = DataStoreService:GetDataStore("FirstJoin")
local FirstJoin
game.Players.PlayerAdded:Connect(function(Player)
local success, errorMessage = pcall(function()
FDW = DataStoreFDW:GetAsync(Player.UserId)
DailyReward = DataStoreDailyReward:GetAsync(Player.UserId)
DailyReward_1 = string.gsub(DailyReward, DailyReward:sub(12,-5), "")
DailyReward_2 = string.gsub(os.date(), os.date():sub(12,-5), "")
FirstJoin = DataStoreFirstJoin:GetAsync(Player.UserId)
if FirstJoin == nil then
FirstJoin = true
else
if FirstJoin then
FDW = 0
FirstJoin = false
DataStoreFirstJoin:SetAsync(Player.UserId, FirstJoin)
if DailyReward_1 ~= DailyReward_2 then
FDW = FDW + 50
DataStoreDailyReward:SetAsync(Player.UserId, os.date())
end
else
if DailyReward_1 ~= DailyReward_2 then
FDW = FDW + 50
DataStoreDailyReward:SetAsync(Player.UserId, os.date())
end
end
end
end)
if not success then
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local success, errorMessage = pcall(function()
DataStoreFDW:SetAsync(Player.UserId, FDW)
end)
if not success then
warn(errorMessage)
end
end)
while wait() do
RemoteEventFDW:FireAllClients(FDW)
end
local Player = game.Players
local DataStoreService = game:GetService("DataStoreService")
local DataStoreFirstJoin = DataStoreService:GetDataStore("FirstJoin")
local FirstJoin
Player.PlayerAdded:Connect(function(player)
FirstJoin = DataStoreFirstJoin:GetAsync(player.UserId)
if FirstJoin == nil then
--first time
FirstJoin = false
DataStoreFirstJoin:SetAsync(player.UserId, FirstJoin)
else
--not first time
end
end)