You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve?
I want to loop through every item in a player’s data table. -
What is the issue?
My for loop is only going through one item, and not every item in the table. -
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.