Table not adding full string

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)
		GunsSave:SetAsync(Player.UserId, GunsFolder)

Can you print the table and show output?

I’d it before but it’s incomplete. “Rifle”

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)
-- 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

-- 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()

It’s “Assault Rifle”… And I wouldn’t skip strings with spaces.

you said its printing Rifle so i assume its saved as Rifle in the datastore now so you need to remove that from the datastore

i also don’t think you should skip strings with spaces and i never told you to skip strings with spaces

1 Like

I’d clear my data and it’s working, thank you!

if for some reason the incorrect value gets added to the datastore again you will get the same error again

that’s where my demo code i sent above can shows you how to prevent it from happening again in the future

1 Like