For Loops won't run while saving things to a data store?

No problem!

For the player joining part do (if you didn’t set up the game.Players.PlayerAdded Event:

If you save the tools as an object to the table like

local whackerValues = {}
	
for _, v in ipairs(game.ReplicatedStorage.Whackers:GetChildren()) do
	whackerValues[v.Name] = v -- just "v" instead because it saves the actual object and you want to load the tools back into their inventory (i'm assuming)
end

then you can do

local Data = myDataStore:GetAsync(player.UserId.."-whackers")

for k, v in pairs(Data) do
    v.Parent = player.Backpack
end

So I know how to load the cash and stuff but I’m finding loading the tools a bit more difficult. I had created a folder inside the player called “DataFolder” and inside that folder is multiple IntValues with the name of a their corresponding whacker’s name then “_Id” and now in the loading the values in the data store, I have loaded the cash but how would I loop through my data table then change the values in the folder to what the values would equal to?

Here is the code:

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local DataFolder = Instance.new("Folder", player)
	DataFolder.Name = "DataFolder"
	
	for i,v in pairs(game.ReplicatedStorage.Whackers:GetChildren()) do
		local WeaponId = Instance.new("IntValue", DataFolder)
		WeaponId.Name = (v.Name.."_Id")
	end
	
	local cash = Instance.new("IntValue", leaderstats)
	cash.Name = "Coins"
	
	local Cashdata
	local Tooldata

	local success, errormessage = pcall(function()
		Cashdata = myDataStore:GetAsync(player.UserId.."-cash")
		Tooldata = myDataStore:GetAsync(player.UserId.."-whackers")
		
		
	end)
	
	if success then
		cash.Value = Cashdata

		
		print("Got player data successfully!")
	else
		print("There was an error while getting player's data")
		warn(errormessage)
	end
end)

If you don’t understand I will try to explain more, you are basically making sure my game works lol (You’re being a massive help)!

I would personally do

for index, value in pairs(Tooldata) do
    if DataFolder:FindFirstChild(index .. "_Id") then
       DataFolder[index.."_Id"].Value = value
    end
end

Ok I did this but it sets the value inside the data folder to a string of the value of the tool data and the value inside the data folder is a IntValue, not a StringValue. So it ends up not changing the value. So is there a way I can like take the last part of the value in the tool data store where there is only numbers and not have the name of the whacker_?

Wait, does it need to be a specific id or is it any number?

Well when you purchase the tool from the shop it changes the Id number in the DataFolder to 17 so if an exploiter ever manages to start changing values, its a pretty random number

Okay, if the object being saved is an IntValue then do this instead:

for index, value in pairs(Tooldata) do
    if DataFolder:FindFirstChild(index .. "_Id") then
       DataFolder[index.."_Id"].Value = value.Value
    end
end

(Keep in mind that when you save objects, everything in the object is saved (including children and descendants)

also for security, I would recommend using RemoteEvents to change the id number

But if the object is a Tool then create an IntValue as a child for it as reference for an id and set the DataFolder Object value to that value

1 Like

I got everything to work! Whenever I join the game I get the items I bought last go-around. I also answered this thread: The reason why the loop wasn’t printing anything was that whenever I bought the tool, I made a clone of the tool but then that cloned tool is floating in the void and I ended up putting the original tool into the backpack, so when the loop runs when the game ends, there is nothing in the folder to actually loop through. I’m not sure how I can thank you enough man, I was totally sure I would have to like ditch this game project because what was going to keep players coming back? There items weren’t being saved but now with your help I’ve made a system where I don’t have to save and load a variable for every single tool. You are a hero my man and if I ever need help I know to come to you. Thank you!

here is the code that helped complete the system

for index, value in pairs(Tooldata) do
			print(index, value)
			for _, tool in pairs(game.ReplicatedStorage.Whackers:GetChildren()) do
				if value == (tool.Name.."_17") then
					local Tool = tool:Clone()
					local bTool = tool:Clone()
					
					bTool.Parent = player.Backpack
					Tool.Parent = player.StarterGear
					
					player.DataFolder:FindFirstChild(tool.Name.."_Id").Value = 17
				end
			end
			
		end
1 Like

No problem, glad I could help!