Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

[SOLVED] DataStore2 Problem! Saving Folder Values, failure?

Hello everyone!

I've been trying to impliment DataStore2 in saving my items in game! The items are in a table or folder if you will, placed inside the LocalPlayers Leaderstats

It saves the coins without any trouble, but won't save the Items list as it gives the Infinite Yield error code. I am assuming that the problem is where the items are boolean values, and I just don't entirely know how to go about datastoring these boolean values, so I've turned to here!

My code below will provide my attempt at doing so, in which caused the error statement to pop up!

Thank you for everyones help, and I appreciate it!

My Script:

local players = game:GetService("Players")

local stats = {
	{"Coins", 20000},
	{"Items", 0},
	{"Inventory", nil}
}

players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	for i,v in pairs(stats) do
		local statType = "IntValue"
		local name = v[1]
		local value = v[2]
		if type(value) == "boolean" then
			statType = "BoolValue"
		elseif type(value) == "string" then
			statType = "StringValue"
		elseif value == nil then
			statType = "Folder"
		end
		local stat = Instance.new(statType)
		stat.Name = name
		if value then stat.Value = value end
		stat.Parent = folder
	end
	
	local DataStore2 = require(game.ServerScriptService:WaitForChild("MainModule"))
	
	local coinsDataStore = DataStore2("coins",player)
	local itemsDataStore = DataStore2("items",player)
	
	
	local function coinsUpdate(updatedValue)
		folder:WaitForChild("Coins").Value = coinsDataStore:Get(updatedValue)
	end
	
	coinsUpdate(folder:WaitForChild("Coins").Value)
	coinsDataStore:OnUpdate(coinsUpdate)
	
	local function itemsUpdate(updatedValue)
		folder:WaitForChild("items").Value = itemsDataStore:Get(updatedValue)
	end
	
	itemsUpdate(folder:WaitForChild("items").Value)
	itemsDataStore:OnUpdate(itemsUpdate)
	
end)

1 answer

✓ Accepted answer
folder:WaitForChild("items")

Assuming I read your code correctly, you specifically named your leaderstat "Items" with a capital "I". The correct fix would be:

folder:WaitForChild("Items")

It is erroring because the object that it is looking for doesn't exist.

1 comment

0

Sometimes it's something dumb, lol. Thanks so much!

This discussion is preserved as it was. New answers, comments, and votes are closed.