Script Problem!

  1. What do you want to achieve?
    I want to know what’s the problem with this script !

  2. What is the issue?
    I get this error :
    Infinite yield possible on 'FireStrykerAzul:WaitForChild("PlayerGui")'

  3. What solutions have you tried so far?

local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")

local Players = game:GetService("Players")

local Success, CurrentPlayerData

Players.PlayerAdded:Connect(function(Player)
	repeat
		Success, CurrentPlayerData = pcall(function()
			return PlayerData:GetAsync(Player.UserId)
		end)
	until Success
	if CurrentPlayerData == nil then
		repeat
			Success = pcall(function()
				PlayerData:SetAsync(Player.UserId, {{}, {}})
			end)
		until Success
	end
	game:BindToClose((function()
		local MyServerList = Player:WaitForChild("PlayerGui").Menu.MyServerList
		local SavingValue
		for i, Child in pairs(MyServerList:GetChildren()) do
			print(Child)
			if Child:IsA("GuiButton") then
				SavingValue = string.split(Child.TextLabel.Text, " / ")
			end
		end
		table.insert(CurrentPlayerData[1], SavingValue)
		repeat
			Success = pcall(function()
				PlayerData:SetAsync(Player.UserId, CurrentPlayerData)
			end)
		until Success
	end))
end)

Don’t use WaitForChild(), try using FindFirstChild() and then printing it. If it comes back nil that means that the PlayerGui doesn’t exist and you should probaly look into autosaving it or saving it on change.

4 Likes

I have the same problem, as soon as the child of PlayerGui change it turn to nil but if there is’nt change that work …

Anybody know the answer ?
Thank you very much if you can help me !

Seems like the player was removed before BindToClose fired. Make sure the player is valid with Player and Player.Parent == Players.

The other post is right you should look into saving at other times, BindToClose is only when the Server shuts down, which may take days for semi-popular games. Also the player is very likely to have left the game when it fires, you would at best have one valid player left.

1 Like

If I check only if Player then it pass but when I check too if the parent of Player is Players that doesn’t pass !

You mean that I need to do player removed ?

Right, objects being destroyed are still valid (not nil) but their parents will be set to nil and locked. So your player reference is invalid and shouldn’t be used. You could try saving on Players.PlayerRemoving but any yielding function (such as WaitForChild) will invalidate your player reference again. You have to be careful to get as much data saved to local variables before using yielding functions and SetAsync.

1 Like

I try it and I get the same error :

local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")

local Players = game:GetService("Players")

local Success, CurrentPlayerData

Players.PlayerAdded:Connect(function(Player)
	repeat
		Success, CurrentPlayerData = pcall(function()
			return PlayerData:GetAsync(Player.UserId)
		end)
	until Success
	if CurrentPlayerData == nil then
		repeat
			Success = pcall(function()
				PlayerData:SetAsync(Player.UserId, {{}, {}})
			end)
		until Success
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local MyServerList = Player:FindFirstChild("PlayerGui").Menu.MyServerList
	local SavingValue
	for i, Child in pairs(MyServerList:GetChildren()) do
		if Child:IsA("GuiButton") then
			SavingValue = string.split(Child.TextLabel.Text, " / ")
		end
	end
	table.insert(CurrentPlayerData[1], SavingValue)
	repeat
		Success = pcall(function()
			PlayerData:SetAsync(Player.UserId, CurrentPlayerData)
		end)
	until Success
end)

I still need help, PLEASE !!!
Have a nice day !