Basically, I need to index items by it’s name, but I can’t find items like “Assault Rifle”.
Error line:
local FoundGuns = GunsSave:GetAsync(Player.UserId)
for Position, Gun in ipairs(FoundGuns) do
print(Gun .. Position)
local GunClone = game.ReplicatedStorage.Guns:FindFirstChild(Gun):Clone() -- ServerScriptService.Script:9: attempt to index nil with 'Clone'
Also this inserts strings.
local GunsFolder = {}
for Position, Gun in pairs(Guns:GetChildren()) do
table.insert(GunsFolder, Gun.Name)
end
GunsSave:SetAsync(Player.UserId, GunsFolder)
i think you problem might be that you use to have a gun called Rifle but now you don’t so it can no longer find it
-- create a empty table
local gunTable = {}
-- loop every instance inside the gun folder
for i, gun in ipairs(gunsFolder:GetChildren()) do
-- add the instances name to the gun table
table.insert(gunTable, gun.Name)
end
-- save the gun table to the datastore
local success, value = pcall(gunsStore.SetAsync, gunsStore, player.UserId, gunTable)
-- create a empty table
local guns = {}
-- loop every gun inside the Guns folder
for i, gun in ipairs(game.ReplicatedStorage.Guns:GetChildren()) do
-- save the gun to the guns table using the guns name as the key (its impotent that every gun has a unique name)
guns[gun.Name] = gun
end
-- load the data from the datastore
local success, value = pcall(gunsStore.GetAsync, gunsStore, player.UserId, gunTable)
-- if success is false then the datastore server might be down
if success = false then return end
-- set gunTable to value but if value is equal to nil then set it to a new empty table
local gunTable = value or {}
-- loop every name in the gun table
for i, gun in ipairs(gunTable) do
-- if we cant find the gun in the guns table that means the gun was not found in game.ReplicatedStorage.Guns so we continue which means we skip this item in the list
if guns[gun] == nil then continue end
-- if the gun was found then we clone it
local gunClone = guns[gun]:Clone()
end