Data store script stopped working

I made this data store script a while ago based on this tutorial and it worked fine. Now I’ve been gone from roblox a few months and the script is giving me an error message “ServerScriptService.Script:33: attempt to index nil with number”, it’s on the line 33. I don’t remember if I did something the last time I was in studio, but I have no idea what is wrong with this as I don’t fully understand the

pcall(dataStore.GetAsync,dataStore,playerKey)

Here is the whole script. Line 33 is the “skinColor.Value = Color3.new(retValue[1],retValue[2],retValue[3]) or Color3.new” part

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("dataStore1")
local players = game:GetService("Players")
local runService = game:GetService("RunService")

local function waitForRequestBudget (requestType)
	local currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
	while currentBudget < 1 do
		currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
		wait(5)
	end
end

------------LOAD DATA-----------------
local function setUp(player)
	local userId = player.UserId
	local playerKey = userId 

	local leaderStats = Instance.new("Folder")	
	leaderStats.Name = "Stats"

	local skinColor = Instance.new("Color3Value")
	skinColor.Name = "SkinColor"

	local success,retValue

	repeat
		waitForRequestBudget(Enum.DataStoreRequestType.GetAsync) 
		success,retValue = pcall(dataStore.GetAsync,dataStore,playerKey) 
	until success or not players:FindFirstChild(player.Name)

	if success then 
		skinColor.Value = Color3.new(retValue[1],retValue[2],retValue[3]) or Color3.new
		skinColor.Parent = leaderStats
		leaderStats.Parent = player
		print("Data loaded")
	end
end


----------SAVE DATA--------------
local function save(player, leaving)
	local userId = player.UserId
	local playerKey = userId

	local leaderstats = player:FindFirstChild("Stats")

	if leaderstats then
		local skinColorValue = leaderstats.SkinColor.Value
		local skinColorTable = {skinColorValue.r,skinColorValue.g,skinColorValue.b}

		repeat
			if not leaving then
				waitForRequestBudget(Enum.DataStoreRequestType.SetIncrementAsync)
			end
			local success, retValue = pcall(dataStore.UpdateAsync, dataStore, playerKey, function()
				return skinColorTable
			end)
			if success then
				print("data succesfully saved")
			end
		until success
	end
end
-----------------------------------------------------------------
local function onShutDown()
	if runService:IsStudio() then 
		wait(2)
	else   							
		local finished = Instance.new("BindableEvent")
		local allPlayers = players:GetPlayers()
		local leftPlayers = #allPlayers 

		for _, player in ipairs(allPlayers) do
			coroutine.wrap(function()
				save(player,true)
				leftPlayers -= 1
				if leftPlayers == 0 then
					finished:Fire()
				end
			end)
		end
		finished.Event:Wait()
	end
end

game.Players.PlayerAdded:Connect(setUp)
game.Players.PlayerRemoving:Connect(save)
game:BindToClose(onShutDown)

while true do	--Auto save--
	wait(300)
	for _,player in ipairs(players:GetPlayers()) do
		coroutine.wrap(setUp)(player) 
	end
	for _,player in ipairs(players:GetPlayers()) do	
		coroutine.wrap(save)(player)
	end
end
1 Like
	if success then 
		local DefaultColor = Color3.new(1,1,1) --Change to intended default color
		if typeof(retValue) == "table" then --Makes sure retValue is not nil.
			if retValue[1] and retValue[2] and retValue[3] then --Makes sure there are 3 values available in the table.
				skinColor.Value = Color3.new(retValue[1],retValue[2],retValue[3]) 
			else
				skinColor.Value = DefaultColor
			end
		else
			skinColor.Value = DefaultColor
		end
		skinColor.Parent = leaderStats
		leaderStats.Parent = player
		print("Data loaded")
	end
1 Like