Script problem!

I don’t understand why this code doesn’t work … :neutral_face:

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.

2 Likes

There are no error in the output and the code doesn’t work at this part :

if DataStoreFirstJoin:GetAsync(Player.UserId) == nil then

Thank for the reply, have a nice day ! :sunglasses:

Bruh? What did you expect when you put a pcall around the function

By the way did you turn on API requests?

1 Like

I use pcall to set data store ! :grinning:
And yes I’ve turn on API requests ! :smiley:

1 Like

The problem is when I want to print for know if my value is nil it does not work, I’m really lost because it’s the first time I use data store … :neutral_face:

1 Like

Are there any errors or things that can occur an error in your script any chance?

1 Like

No there is no error ! :neutral_face: I’m really lost sorry if it’s poor explained I need to take a pause ! :sweat_smile:
Have a good day ! :grinning:

1 Like

Unfortunately, I don’t know what’s the problem too, sorry for that. Anyways, have a good day! :wave:

1 Like

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

1 Like

pcall functions are used to run code that might fail, so you won’t get an error in the output unless you want it. Try something like this:

local success, errorMessage = pcall(function()
    -- do stuff
end)
if not success then
    warn(errorMessage)
end
1 Like

I’ve do this but that doesn’t work … :neutral_face: I think the problem is when I check if FirstJoin is nil … :thinking:

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

I get this error :
ServerScriptService.Script:24: attempt to index boolean with 'sub'
Thank you for the help have a nice day ! :grinning:

Can you try printing DataStoreDailyReward:GetAsync(Player.UserId) and see what it returns?

1 Like

That work fine, this is not the problem, the problem is from I’ve add the Datastore first join that won’t work ! :sweat_smile:

And I’ve remake my script and it work :

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)

Have a nice day ! :grin: