Line 82 error, causing datastore failure

I am making a bee swarm simulator fan-game, but every time i convert pollen to honey, the datastore mentions an error on line 82. it will not save.
code:

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

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

function PlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	local honey = Instance.new("IntValue")
	honey.Name = "Honey"
	honey.Parent = leaderstats
	
	local pollen = Instance.new("IntValue")
	pollen.Name = "Pollen"
	pollen.Parent = leaderstats
	
	local digrate = Instance.new("NumberValue")
	digrate.Name = "Dig Rate"
	digrate.Parent = player
	
	local digspeed = Instance.new("NumberValue")
	digspeed.Name = "Dig Speed"
	digspeed.Parent = player
	
	local bees = Instance.new("NumberValue")
	bees.Name = "Bees"
	bees.Parent = leaderstats
	
	local wpolmul = Instance.new("NumberValue")
	wpolmul.Name = "White Pollen Multiplier"
	wpolmul.Parent = player
	local rpolmul = Instance.new("NumberValue")
	rpolmul.Name = "Red Pollen Multiplier"
	rpolmul.Parent = player
	local bpolmul = Instance.new("NumberValue")
	bpolmul.Name = "Blue Pollen Multiplier"
	bpolmul.Parent = player
	local newbie = Instance.new("ObjectValue")
	newbie.Name = "New Bie"
	newbie.Value = nil
	newbie.Parent = 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 == 10
	
	if success then
		print("Connected to database")
		if not playerData then
			print("Assigning default data")
			playerData = {
				["Honey"] = 0,
				["DigRate"] = 0.1,
				["DigSpeed"] = 4,
				["WPolMul"] = 1,
				["RPolMul"] = 1,
				["BPolMul"] = 1,
				["Hive"] = {"Basic Bee"},
				["Bees"] = 1
			}
		end
			newbie.Changed:Connect(function()
				table.insert(playerData[player.UserId].Hive, newbie.Value.Name)
				game.ReplicatedStorage.LoadBees:FireClient(player, playerData.Hive)
				newbie.Value = nil
			end)
		honey.Value = playerData.Honey
		honey.Changed:Connect(function()
			playerData[player.UserId].Honey = honey.Value
		end)
		bees.Value = playerData.Bees
		bees.Changed:Connect(function()
			playerData[player.UserId].Bees = bees.Value
		end)
		digrate.Value = playerData.DigRate
		digrate.Changed:Connect(function()
			playerData[player.UserId].DigRate = digrate.Value
		end)
		digspeed.Value = playerData.DigSpeed
		digspeed.Changed:Connect(function()
			playerData[player.UserId].DigSpeed = digspeed.Value
		end)
		wpolmul.Value = playerData.WPolMul
		wpolmul.Changed:Connect(function()
			playerData[player.UserId].WPolMul = wpolmul.Value
		end)
		rpolmul.Value = playerData.RPolMul
		wpolmul.Changed:Connect(function()
			playerData[player.UserId].RPolMul = rpolmul.Value
		end)
		bpolmul.Value = playerData.BPolMul
		wpolmul.Changed:Connect(function()
			playerData[player.UserId].BPolMul = bpolmul.Value
		end)
		print(playerData.Hive)
		game.ReplicatedStorage.LoadBees:FireClient(player, playerData.Hive)
		game.ReplicatedStorage.GiftBees.OnServerEvent:Connect(function(Player)
			game.ReplicatedStorage.ClaimBees:FireClient(player, playerData.Hive)
		end)
	else
		warn("Unable to get data for", player.Name)
		player:Kick("Unable to load data, please rejoin if you can")
	end
	
	leaderstats.Parent = player
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 == 10
		
		if success then
			print("Data saved for", player.Name)
		else
			warn("Unable to save for", player.Name)
		end
	end
end
Players.PlayerRemoving:Connect(PlayerLeaving)

Can you post what the error is?

1 Like

14:21:24.523 ServerScriptService.Datastores:82: attempt to index nil with ‘Honey’ - Server - Datastores:82
14:21:24.524 Stack Begin - Studio
14:21:24.524 Script ‘ServerScriptService.Datastores’, Line 82 - Studio - Datastores:82
14:21:24.524 Stack End - Studio

Thanks. That’s probably because you’re indexing playerData with player.UserId even though that key doesn’t exist in the table you defined. You defined playerData as

playerData = {
    ["Honey"] = 0,
    ["DigRate"] = 0.1,
    ["DigSpeed"] = 4,
    ["WPolMul"] = 1,
    ["RPolMul"] = 1,
    ["BPolMul"] = 1,
    ["Hive"] = {"Basic Bee"},
    ["Bees"] = 1
}

You should be doing playerData.Honey = honey.Value instead of playerData[player.UserId].Honey = honey.Value, and that would apply for all the other values in your table as well.

2 Likes

i see.
i did infact use the gnomecode datastore tutorial.

another problem…
now it wont save!

In PlayerLeaving, you’re looking for the player’s data in sessionData. You never set that in PlayerAdded so it will never save anything.

1 Like

that was supposed to be where it was set.
how can i set it?

Is this the tutorial you were following? https://youtu.be/H-cDbjd5-bs?si=ylHzdlbHn6rbfp9S&t=659

In PlayerAdded, he writes sessionData[player.UserId] = playerData after the data successfully loads. You forgot to include it in your script.

Edit: In fact, after watching that video I see where your original mistake was. Instead of doing playerData[player.UserId], you should’ve been doing sessionData[player.UserId].

it was.
im gonna do that.
thanks :smiley:

new line 77 error.
attempt to index nil with name.
table.insert(playerData.Hive, newbie.Value.Name)
edit: nevermind. thats just an error.
it doesnt mean anything.
it still works :smiley:

You’re explicitly setting newbie.Value to nil

local newbie = Instance.new("ObjectValue")
newbie.Name = "New Bie"
newbie.Value = nil
newbie.Parent = player

So it makes sense you’re getting that error. You can’t access the Name property on a nil value. I believe what you were intending to do is newbie.Name instead of newbie.Value.Name

1 Like

we dont really care about the error anymore, it means nothing.
that is intended, because we can get a thousand basic bees, but if we dont do that, we can only get 1.

1 Like

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