Lua code only loops through one item in a table

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I want to loop through every item in a player’s data table.

  2. What is the issue?
    My for loop is only going through one item, and not every item in the table.

  3. What solutions have you tried so far?
    I have tried changing it from an in pairs loop to a normal for loop, and to an if statement, but nothing worked since I keep getting the same issue.

I’ll now describe what I’m doing in one of my scripts. So I have a Cosmetic system where players can buy different gears with in-game currency, so when a player buys or looks through the purchasable cosmetics, I use a RemoteFunction where on each invoke, I check if the player has any specified value in their DataStore table. If the value specified in the RemoteFunction’s parameters are found, it returns true, If not then it returns false. If an error occurs within the function’s code then it prints the error message and returns nil.

However, I have been experiencing an issue where in a for loop, it seems to stop at one item only.

If anyone is able to help, then thank you so much!

Here’s the script:

local dataStoreService = game:GetService("DataStoreService")
local cosmeticsData = dataStoreService:GetDataStore("CosmeticData")

local replicatedStorage = game:GetService("ReplicatedStorage")

local rfs = replicatedStorage:WaitForChild("RemoteFunctions")

local cosmetics = require(replicatedStorage.Modules:WaitForChild("Cosmetics"))
local cTools = replicatedStorage:WaitForChild("Cosmetics")

rfs.GetCosmeticOwned.OnServerInvoke = function(player, cosmeticToolName)
	local cosmeticKey = player.UserId.."-cosmeticData"
	local success, data = pcall(function()
		return cosmeticsData:GetAsync(cosmeticKey)
	end)
	if success then
		print("Data for the player was succesfully retrieved, check if the data exists")
		if data ~= nil then
			print("The data exists, check if the tool's name exists via a for loop")
			for i = 1, #data do
				if data[i] == cosmeticToolName then
					print("Cosmetic owned")
					return true
				else
					print("Cosmetic not owned")
					return false
				end
			end
		else
			print("No data, will automatically return false")
			return false
		end
	else
		print("Failed:", data)
		return nil
	end
end

If needed to, I’ll edit this topic to try and describe it more clearly. Again, if anyone is able to help, then thanks so much!

If I find a solution myself, I’ll make a reply about the solution I found and mark the reply as a solution.

Try printing I in the line right after the loop starts

Alright, I’ve done that and this is what I got:

15:45:00 -- 1

Seems to only print 1 and not any other numbers.

why not try using for i,v in pairs?

I’ve already done that before. Same results.

I’ve even listed it in the solutions I tried in my topic, but I will give it one more try.

Edit: I tried it again and I still got the same results.

You are returning in the loop. This ends the loop and the whole function. Once we hit return, nothing else in the function runs. Ditch the return false and move that to outside of the for loop or, better yet, move it all the way to the bottom of the function. You only need one return that way, and it ensures that the function always returns something. This can be important in other languages. I noticed you’re also returning nil sometimes though, if that’s important to you then just return false at the end of the for loop like this:

		if data ~= nil then
			print("The data exists, check if the tool's name exists via a for loop")
			for i = 1, #data do
				if data[i] == cosmeticToolName then
					print("Cosmetic owned")
					return true
				else
					print("Cosmetic not owned")
				end
			end
            return false
		else
			print("No data, will automatically return false")
			return false
		end

Thanks for the help! It’s working perfectly now.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.