Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Output saying Data isnt a table?!

So I've been attempting to data save hats for the past couple of days. And now I'm getting this error that says table expected got nil on line 21, I am also getting the same error in player removing but instead of nil it says it got an object. I dont get how to fix this. Here it is

local dss = game:GetService("DataStoreService")
local myDataStore = dss:GetDataStore("Clothes")

game.Players.PlayerRemoving:Connect(function(plr)
	local Wear = {}
	for _,item in pairs(plr.Items) do
		table.insert(Wear,item.Name)
	end
	pcall(function()
		myDataStore:SetAsync(plr.UserId,Wear)
	end)
end)

game.Players.PlayerAdded:Connect(function(plr)
	local data
	local Folder = Instance.new("Folder",plr)
	Folder.Name = "Items"
	pcall(function()
		data = myDataStore:GetAsync(plr.UserId)
	end)
	for _,item in pairs(data) do
		local Value	= Instance.new("StringValue",Folder)
		Value.Name = item
	end
	plr.CharacterAdded:Connect(function()
		for i, accessory in pairs(Folder) do
			game.ServerStorage.Everything[accessory.Name]:Clone().Parent = plr.Character
		end
	end)
end)
	

1 answer

Have you ensured the data exists yet? The data variable is clearly nil at line 15 so if the GetAsync() returned nil (no data found) then it would remain nil and error. Just make the data variable a blank table at line 15 and then only change the data variable if GetAsync() returns something that isn't nil.

3 comments

0

how would i change the data variable if get async returns something that isn't nil

0

i made data into a table

0

Just add an if statement that checks if datastore:GetAsync(id) ~= nil then you make it data = datastore:GetAsync(id)

This discussion is preserved as it was. New answers, comments, and votes are closed.