Help with my datastore!

I have this error when server is started, I believe it is causing all scripts relevant to “Wins” to fail:
ServerScriptService.Data:100: attempt to index nil with 'Wins'

local Players = game:GetService("Players")
local DatastoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")

local database = DatastoreService:GetDataStore("TestData2")
local sessionData = {}

local defaultData = {
	--leaderstats
	["Wins"] = 0,
	
	--towers
	["Anime"] = true,
	["Superhero"] = false,
}

task.wait(.03)

function PlayerAdded(player)
	local character = player.Character or player.CharacterAdded:Wait()
	
	createdata(player)
	
	local success = nil
	local playerData = nil
	local attempt = 1

	repeat
		success, playerData = pcall(function()
			return database:GetAsync(player.UserId)
		end)

		attempt += 1

		if not success then
			warn(playerData)
			task.wait(3)
		end
	until success or attempt == 5

	if success then
		if not playerData then
			playerData = defaultData
		end
		sessionData[player.UserId] = playerData
	else
		warn("Unable to get Data for", player.UserId)
		player:Kick("Unable to load your data. Try again later")
	end
end

Players.PlayerAdded:Connect(PlayerAdded)

function PlayerLeaving(player)
	if sessionData[player.UserId] then
		local success = nil
		local errorMsg = nil
		local attempt = 1

		repeat
			success, errorMsg = pcall(function()
				database:SetAsync(player.UserId, sessionData[player.UserId])
			end)
			attempt += 1

			if not success then
				warn(errorMsg)
				task.wait(3)
			end
		until success or attempt == 5

		if success then
			print("Data saved for", player.Name)
		else
			warn("Unable to save for", player.Name)
		end
	end
end

Players.PlayerRemoving:Connect(PlayerLeaving)

function ServerShutdown()
	if RunService:IsStudio() then
		return
	end
	print("Server Shutting Down!")
	for i, player in ipairs(Players:GetPlayers()) do
		task.spawn(function()
			PlayerLeaving(player)
		end)
	end
end

game:BindToClose(ServerShutdown)

function createdata(player)
	--create values
	
	print(print(sessionData[player.UserId]))
	print(sessionData[player.UserId].Wins) -- continue
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Parent = leaderstats

	local playerdata = Instance.new("Folder")
	playerdata.Name = "PlayerData"
	playerdata.Parent = player

	local towerfolder = Instance.new("Folder")
	towerfolder.Name = "Tower"
	towerfolder.Parent = playerdata

	local itemfolder = Instance.new("Folder")
	itemfolder.Name = "Item"
	itemfolder.Parent = playerdata

	local robuxfolder = Instance.new("Folder")
	robuxfolder.Name = "Robux"
	robuxfolder.Parent = playerdata

	local animevalue = Instance.new("BoolValue")
	animevalue.Name = "anime"
	animevalue.Parent = towerfolder

	local superherovalue = Instance.new("BoolValue")
	superherovalue.Name = "superhero"
	superherovalue.Parent = towerfolder
	
	--set values
	
	wins.Value = sessionData[player.UserId].Wins
	wins.Changed:Connect(function()
		sessionData[player.UserId].Wins = wins.Value
	end)
	
	animevalue.Value = sessionData[player.UserId].Anime
	animevalue.Changed:Connect(function()
		sessionData[player.UserId].Anime = animevalue.Value
	end)
	
	superherovalue.Value = sessionData[player.UserId].Superhero
	superherovalue.Changed:Connect(function()
		sessionData[player.UserId].Superhero = superherovalue.Value
	end)
end

Any feedback of anykind is greatly appreciated

1 Like

I dont know which line is line 100, but im going to assume its the one printing the .Wins first. this is happening because when you call a function in Lua, its waits for that function to return. So, it waits until the createdata function is done before going into the loop of getting their data. Putting the create data function call after data has been gotten should fix it.

2 Likes

Thank you so much! This is super helpful.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.