Items don't get bought when pressing buy button

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)

pls help

that warning appears when using WaitForChild().you can use FindFirstChild() instead of WaitForChild().

1 Like

I believe the error is inside this module, as I cannot find anywhere in the code where that line occurs.

Can you send that module?

“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.

1 Like
local conf = game.ReplicatedStorage:WaitForChild("Configuration")
local currencyName = conf:WaitForChild("CurrencyName")
local startingCurrency = conf:WaitForChild("StartingCurrency")


function CreateLeaderstats(plr: Player, plrCash: number)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local currency = Instance.new("IntValue")
	currency.Name = currencyName.Value
	currency.Value = plrCash
	currency.Parent = leaderstats

	return leaderstats
end

return CreateLeaderstats

Here you go

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?

Here’s the explorer

image

There is nothing in SavingData named "CreateLeaderstats*. Are you looking in the wrong place?

Consider using this code instead:

ServerScriptService.ShopServer.ModuleScripts:WaitForChild(“CreateLeaderstats”)
1 Like

ohhh I just realized this probably happens because I didn’t update createleaderstats after changing the saving system

thanks btw

Consider marking my post as a solution if it solved your problem :slight_smile:

1 Like

Done, and one more question

I put that code in SavingData? Or CreateLeaderstats?

Replace that code whereever your error is occuring, I really can’t find it in the scripts you added (bad eyesight)

1 Like

OMG I FEEL SO DUMB AHHH SO SORRY

I found where the warning is coming from!

It was ShopServer.

The code you provided didn’t help btw : (

This script:

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)