[URGENT] How To Deal With Leaderstats Non-Successes?

Error it spits out:

DataStoreService: AccessForbidden: Access forbidden. API: GetAsync, Data Store: LeaderSave

(LeaderSave is my DataStore)


Anytime a DataStore occurs an error and doesn’t success, I kick the player. But once the player rejoins, It becomes 250, instead of their original amount.


Leaderstats Script:

local dataStoreService = game:GetService("DataStoreService")
local DS = dataStoreService:GetDataStore("LeaderSave") -- LeaderSave

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	--------

	local hexa = Instance.new("IntValue")
	hexa.Name = "Hexa"
	hexa.Value = 250 -- Always 100
	hexa.Parent = leaderstats
	

	-------
	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Value = 0
	wins.Parent = leaderstats

	local deaths = Instance.new("IntValue")
	deaths.Name = "Deaths"
	deaths.Value = 0
	deaths.Parent = leaderstats

	-------



	print("Leaderstats Loaded!")

	--------------------------------------------

	local DeathsData, WinsData, HexaData

	local success, errormessage = pcall(function()
	
			DeathsData = DS:GetAsync("deaths-"..player.UserId)
			WinsData = DS:GetAsync("wins-"..player.UserId)
			HexaData = DS:GetAsync("hexa-"..player.UserId)
	

	end)

	if success then
		print("Successful!")
		deaths.Value = DeathsData
		wins.Value = WinsData
		hexa.Value = HexaData
	else
		warn("Failed To Load Data!")
		
		player:Kick("Please rejoin. An error occured with your data")
		

	end


end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		DS:SetAsync("deaths-"..player.UserId, player.leaderstats.Deaths.Value)
		DS:SetAsync("wins-"..player.UserId, player.leaderstats.Wins.Value)
		DS:SetAsync("hexa-"..player.UserId, player.leaderstats.Hexa.Value)
	end)
end)

game:BindToClose(function()
	wait(2)
end)
3 Likes

Have you enabled studio access to api services, if not or you are not sure do the following:

In the project click Game settings → security → Enable studio access to API services

1 Like

It is enabled and has been for a while

Use tostring(player.UserId)
For both load and save
Aaaaaaaaaaaaaaaaaaa

1 Like

Agreed, although that shouldn’t matter

2 Likes

Do any of you guys know on how to deal with when the leaderstat fails?

So I’ll just have to continously loop? Okay.

local dataStoreService = game:GetService("DataStoreService")
local DS = dataStoreService:GetDataStore("LeaderSave") -- LeaderSave

game.Players.PlayerAdded:Connect(function(player)
	local count = 0 --1

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	--------

	local hexa = Instance.new("IntValue")
	hexa.Name = "Hexa"
	hexa.Value = 250 -- Always 100
	hexa.Parent = leaderstats
	

	-------
	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Value = 0
	wins.Parent = leaderstats

	local deaths = Instance.new("IntValue")
	deaths.Name = "Deaths"
	deaths.Value = 0
	deaths.Parent = leaderstats

	-------



	print("Leaderstats Loaded!")

	--------------------------------------------

	local DeathsData, WinsData, HexaData

	local success, response = pcall(function()
	
			DeathsData = DS:GetAsync("deaths-"..tostring(player.UserId))
		WinsData = DS:GetAsync("wins-"..tostring(player.UserId))
		HexaData = DS:GetAsync("hexa-"..tostring(player.UserId))
	

	end)

	if success then
		print("Successful!")
		deaths.Value = DeathsData
		wins.Value = WinsData
		hexa.Value = HexaData
		
	
	end
 
 while not success do
		if count >= 1 then
			warn("Retrying, count:", count, "\nError:", response) --3
			wait(7) --4
		end
	


		success, response = pcall(DS.GetAsync, DS, "hexa-"..tostring(player.UserId), player.leaderstats.Hexa.Value) --5
		success, response = pcall(DS.GetAsync, DS, "deaths-"..tostring(player.UserId), player.leaderstats.Deaths.Value) --5
		success, response = pcall(DS.GetAsync, DS, "wins-"..tostring(player.UserId), player.leaderstats.Wins.Value) --5
		count = count + 1 --6

 end

end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		DS:SetAsync("deaths-"..tostring(player.UserId), player.leaderstats.Deaths.Value)
		DS:SetAsync("wins-"..tostring(player.UserId), player.leaderstats.Wins.Value)
		DS:SetAsync("hexa-"..tostring(player.UserId), player.leaderstats.Hexa.Value)
	end)
end)

game:BindToClose(function()
	wait(2)
end)

@domboss37
@hkep
Would it work?

Although I am skilled in the ability to sight read roblox script behavior, it is better practice for you to use print to find out if your code is working properly.

Give me like 20 mins

1 Like

The datatstore has a slim chance of failing, so it would take a lot of tries…

This while loop doesn’t make sense.
you have 3 pcall function calls but only the 3rd one will be checked if successful.
What I would do is delete this loop and change your earlier code to be in a for loop.

	for i=1,7 do
		local success, response = pcall(function()
			DeathsData = DS:GetAsync("deaths-"..tostring(player.UserId))
			WinsData = DS:GetAsync("wins-"..tostring(player.UserId))
			HexaData = DS:GetAsync("hexa-"..tostring(player.UserId))
		end)
		
		if success then
			print("Successful!")
			deaths.Value = DeathsData
			wins.Value = WinsData
			hexa.Value = HexaData
			break
		end
	end
1 Like


yeah

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.