Guys how can i avoid this?

Soo guys i’m working with gamepass data system but using game.players.playerRemoving dont works really well but game:BindToClose does and saves all the data that i need but when i use bind to close the players removing works too! yes i am testing this on studio cause i dont have alot of robux to just test it in game pog i will aprecciate any help!

local Remote = game.ReplicatedStorage.AddTrail
local DataStore = game:GetService("DataStoreService")
local TrailData = DataStore:GetDataStore("TrailData")




game.Players.PlayerAdded:Connect(function(player)

	local Label = player.PlayerGui:WaitForChild("Shop").Trails.TrailChoosed
	local Trail
	
	local sucess, errormessage = pcall(function()
		
		Trail = TrailData:GetAsync(player.UserId.."-trail")
	 
	end)
	
	if sucess then
		
 if Trail ~= nil then
		Label.Text = Trail
	 end
	end
	
	if errormessage then
		
		print("no data")
	end
	
	Remote:FireClient(player)
	
end)



game.Players.PlayerRemoving:Connect(function(player)
	local Label2 = player.PlayerGui.Shop.Trails.TrailChoosed


	if Label2.Text ~= "None" then		

		local success, errormessage = pcall(function()

			TrailData:SetAsync(player.UserId.."-trail", Label2.Text)


		end)	

		if success then

			print("Saved!")

		end

		if errormessage then

			print("NotSaved!")

		end

	end	
end)
1 Like

image
.PlayerRemoving doesn’t work in a normal studio test, so I would use a Local Server in Studio to test if it works. It’s in the Test Tab.

1 Like

omg i think i accidentally hit the limit of data lol did i need to make another game and like copy and paste the project or the data requests will eventually come back to the normal?

1 Like

but it’s because the set async function was running twice new i think it should work fine but the datas just dont work lol (the get async does) i dont made it before cause if i make any kind of script that stop them to work twice the data just was not saving it, welp if u can help me with this ima be grateful i just want to now if they will eventually come back to normal

1 Like

Greetings, I have read over your conversation.
They should come back soon. If not I would suggest you open a new studio and put your project in a new studio.

Message me if you need any help!

  • J_osq
2 Likes

pcall means protect-call, because it can be used to safely call a function.
I would also suggest rather than indexing services, to use ServiceProvider:GetService.

If the first variable of the pcall is false, then the second variable will be the error message (string). However, if your pcall is successful the first variable is true and the second variable is the response that the function is intended to return (ex: return "This is a response.")

local PlayersService = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("TrailData")

PlayersService.PlayerAdded:Connect(function(LocalPlayer)
	local PlayerGui = LocalPlayer:FindFirstChildWhichIsA("PlayerGui") or LocalPlayer:WaitForChild("PlayerGui")
	local Shop = PlayerGui:WaitForChild("Shop")
	local Trails = Shop:WaitForChild("Trails")
	local TrailChoosed = Trails:WaitForChild("TrailChoosed")

	coroutine.resume(coroutine.create(function()
		local success, response = pcall(function()
			return DataStore:GetAsync(string.format("%s-trail", LocalPlayer.UserId))
		end)
		if success ~= true then
			return error("Failed to load data for @" .. LocalPlayer.DisplayName .. " (" .. LocalPlayer.Name .. ")!", 0)
		end
		ReplicatedStorage:WaitForChild("AddTrail"):FireClient(LocalPlayer)
		return print("Successfully loaded data for @" .. LocalPlayer.DisplayName .. " (" .. LocalPlayer.Name .. ")!")
	end))
end)

PlayersService.PlayerRemoving:Connect(function(LocalPlayer)
	local PlayerGui = LocalPlayer:FindFirstChildWhichIsA("PlayerGui")
	local Shop = PlayerGui:FindFirstChild("Shop")
	local Trails = Shop:FindFirstChild("Trails")
	local TrailChoosed = Trails:FindFirstChild("TrailChoosed")

	coroutine.resume(coroutine.create(function()
		if TrailChoosed.Text ~= "None" then
			local success, response = pcall(function()
				return DataStore:SetAsync(string.format("%s-trail", LocalPlayer.UserId), TrailChoosed.Text)
			end)
			if success ~= true then
				return warn("Failed to save data for @" .. LocalPlayer.DisplayName .. " (" .. LocalPlayer.Name .. ")!" .."\n\error message: " .. response)
			end
			return print("Successfully saved data for @" .. LocalPlayer.DisplayName .. " (" .. LocalPlayer.Name .. ")!")
		end
	end))
end)