What do you want to achieve?
I want to know what’s the problem with this script !
What is the issue?
I get this error : Infinite yield possible on 'FireStrykerAzul:WaitForChild("PlayerGui")'
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.
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.
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.
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)