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

Invalid argument #1 to 'pairs' table expected got nil on my dataStore?

so i made a dataStore script in serverscriptservice and its saying at around line 38 that something went wrong? im not sure how to approach it

heres the full script

local DataStore = game:GetService("DataStoreService"):GetDataStore("HahaisdeadSave18")
local saveRemote = game:GetService("ReplicatedStorage"):WaitForChild("Save")


game.Players.PlayerAdded:Connect(function(player)
	
	wait()
	
	local folder = Instance.new("Folder", player)
	folder.Name = "Data"
	
	
	local hasgame = Instance.new("BoolValue", folder)
	hasgame.Name = "HasGame"
	
	local hair = Instance.new("StringValue", folder)
	hair.Name = "Hair"

	local outfit = Instance.new("StringValue", folder)
	outfit.Name = "Outfit"

	local accessory = Instance.new("StringValue", folder)
	accessory.Name = "Accessory"
	
	local Gender = Instance.new("StringValue", folder)
	Gender.Name = "Gender"
	
	local blood = Instance.new("StringValue", folder)
	blood.Name = "Blood"
	
	local spins = Instance.new("IntValue", folder)
	spins.Name = "Spins"
	
	
	local success, errormsg = pcall(function()
		local getData = DataStore:GetAsync(player.UserId)
		wait()
		for i, v in pairs(getData) do
			if i == 1 then
				hasgame.Value = getData[i]
			elseif i == 2 then 
				hair.Value = getData[i]
			elseif i == 3 then
				outfit.Value = getData[i]
			elseif i == 4 then
				accessory.Value = getData[i]
			elseif i == 5 then
				blood.Value = getData[i]
				
			elseif i == 6 then
				spins.Value = getData[i]
			
			end
		end
	end)
	
	if success then
		print("Successfully loaded "..player.Name.."'s data!")
	elseif not success then
		-- FIRST TIME JOINING --
		hasgame.Value = false
		print("hasgame has been changed to false")
		hair.Value = "Men Hair"
		print("hair value has been changed to men hair")
		outfit.Value = "Luffy's outfit"
		print("outfit has been changed to luffys outfit")
		accessory.Value = "Shades"
		print("accessory has been changed to shades")
		Gender.Value = "None"
		print("gender has been changed to none")
		blood.Value = "Burgundy"
		print("blood has been set to burgundy")
		spins.Value = 8 
		
		error("Something went wrong while loading the data of "..player.Name..": "..errormsg)
	end
	
end)

saveRemote.OnServerEvent:Connect(function(player, val)
	DataStore:SetAsync(player.UserId, val)
	print("Successfully saved "..player.Name.."'s data!")
end)

and heres the start of line 38

for i, v in pairs(getData) do
			if i == 1 then
				hasgame.Value = getData[i]
			elseif i == 2 then 
				hair.Value = getData[i]
			elseif i == 3 then
				outfit.Value = getData[i]
			elseif i == 4 then
				accessory.Value = getData[i]
			elseif i == 5 then
				blood.Value = getData[i]
				
			elseif i == 6 then
				spins.Value = getData[i]
			
			end
		end
	end)

ive tried adding a bunch of waits and everything but nothing worked

2 comments

0

Have you tried moving lines 38-54 to after the "if success then"

0

which would mean your "local getData" would be before the pcall, and inside the pcall would just be "getData = ..." to the loaded variable

1 answer

Not specific for this but I would recommend using a library that manages the dataStore for you, I made a library a couple of months ago that handles all of these methods for you. You can find all the information about it here: https://github.com/prooheckcp/ProStore

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