Hi, so I am making a saving system for my shop, and I encounter this error:
16:43:25.876 Infinite yield possible on ‘ServerScriptService.SavingData:WaitForChild(“CreateLeaderstats”)’ - Studio
I have that script in my game already referenced properly, so I don’t know what to do.
local DataStoreService = game:GetService("DataStoreService")
local conf = game.ReplicatedStorage:WaitForChild("Configuration")
local currencyName = conf:WaitForChild("CurrencyName")
local startingCurrency = conf:WaitForChild("StartingCurrency")
local dsKey = conf:WaitForChild("DatastoreKey")
local datastore = DataStoreService:GetDataStore(dsKey.Value)
local CreateLeaderstats = require(game.ServerScriptService.ShopServer.ModuleScripts.CreateLeaderstats)
-- Function to save player data
local function SaveData(player)
if not player then
return
end
local plrKey = player.UserId
local plrCash = player:FindFirstChild("leaderstats") and player.leaderstats[currencyName.Value].Value or 0
-- Collect player's tools data
local tools = {}
for _, tool in ipairs(player.Backpack:GetChildren()) do
table.insert(tools, tool.Name)
end
local plrData = { Cash = plrCash, Tools = tools }
local success, err = pcall(function()
datastore:SetAsync(plrKey, plrData)
end)
if not success then
warn("Error saving " .. player.Name .. "'s (" .. plrKey .. ") data:\n" .. err)
else
print("Data saved successfully for player:", player.Name)
end
end
-- Connect function to player leaving event
game.Players.PlayerRemoving:Connect(function(player)
SaveData(player)
end)
game:BindToClose(function()
for _, player in ipairs(game.Players:GetPlayers()) do
SaveData(player)
end
end)
-- Function to load player data
local function LoadData(player)
if not player then
return
end
local plrKey = player.UserId
local dataFailedWarning = Instance.new("BoolValue")
dataFailedWarning.Name = "DataLoadFailed"
dataFailedWarning.Value = true
dataFailedWarning.Parent = player
local success, plrData = pcall(function()
return datastore:GetAsync(plrKey)
end)
if success and plrData then
CreateLeaderstats(player, plrData.Cash or startingCurrency.Value)
-- Give the player their saved tools
local backpack = player:WaitForChild("Backpack")
for _, toolName in ipairs(plrData.Tools or {}) do
local tool = game.ServerStorage:FindFirstChild(toolName)
if tool then
tool:Clone().Parent = backpack
end
end
dataFailedWarning.Value = false
else
warn("Error loading " .. player.Name .. "'s (" .. plrKey .. ") data.")
end
dataFailedWarning:Destroy()
end
-- Connect function to player joining event
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
LoadData(player)
end)
end)
“Infinite yield possible” usually occurs when using :WaitForChild(). Most likely the “CreateLeaderstats” object isn’t created when the script is loading. Try adding a 3-5 wait function before the script.
I wouldn’t recommend this, as it is unreliable, it’s not good practice. What you should is continue using :WaitForChild(), but figuring out why that instance doesn’t exist. Can you send the explorer?
local conf = game.ReplicatedStorage:WaitForChild("Configuration")
local currencyName = conf:WaitForChild("CurrencyName")
local startingCurrency = conf:WaitForChild("StartingCurrency")
local canBuyMultipleTimes = conf:WaitForChild("CanBuyItemMultipleTimes")
local SavingData = game.ServerScriptService:WaitForChild("SavingData")
local CreateLeaderstats = require(SavingData:WaitForChild("CreateLeaderstats"))
local CreateTools = require(SavingData:WaitForChild("CreateTools"))
local BuyTool = require(SavingData:WaitForChild("BuyTool"))
local remotes = game.ReplicatedStorage:WaitForChild("RemoteEvents")
local buyToolRE = remotes:WaitForChild("BuyTool")
local equippedTools = {}
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
char.ChildAdded:Connect(function(child)
local equippedTool = char:FindFirstChildOfClass("Tool")
equippedTools[plr] = equippedTool and equippedTool.Name
end)
end)
local plrData = SavingData:LoadData(plr)
local plrCash = plrData and plrData.Cash or startingCurrency.Value
local plrTools = plrData and plrData.Tools or {}
CreateLeaderstats(plr, plrCash)
CreateTools(plr, plrTools)
end)
game.Players.PlayerRemoving:Connect(function(plr)
SavingData:SaveData(plr, equippedTools[plr])
end)
game:BindToClose(function()
for _, plr in pairs(game.Players:GetPlayers()) do
SavingData:SaveData(plr, equippedTools[plr])
end
end)
buyToolRE.OnServerEvent:Connect(function(plr, toolName)
BuyTool(plr, toolName)
end)