Attempt to index nil with 'Name'

A private message is associated with this issue to @zblox164, the tutorial author

I’m using a tutorial to manage auto plot saving and claiming, but upon rejoining, I get an error in the output instead of the expected result. I did the fix he did for the error, but a different error keeps popping up in output.

ServerScriptService.ServerModules.PlotManager:16: attempt to index nil with 'Name' - PlotManager:16
  Stack Begin
  Script 'ServerScriptService.ServerModules.PlotManager', Line 16 - function returnPlot - PlotManager:16
  Script 'ServerScriptService.TycoonDataManager', Line 109 - function unloadTycoonData - TycoonDataManager:109
  Script 'ServerScriptService.TycoonDataManager', Line 121 - TycoonDataManager:121
  Stack End

PlotManager (ModuleScript):

local plotManager = {}

function plotManager.assignPlot(loc,plr)
	for i, plt in pairs(loc:GetChildren()) do
		if plt.Owner.Value == "" then
			plt.Owner.Value = plr.Name
			break
		end
	end

end

function plotManager.returnPlot(loc,plr)
	print(loc)
	for i, plt in pairs(loc:GetChildren()) do
		if plt.Owner.Value == plr.Name then
			return plt
		end
	end
end

return plotManager

TycoonDataManager (ServerScript):

local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")

local plotManager = require(script.Parent.ServerModules.PlotManager)

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("TycoonProgress2")

local tries = 3
local dataloaded = nil

local function serialize(plr)
	if dataloaded then
		local plot = plotManager.returnPlot(workspace.Plots, plr)
		local key = plr.UserId
		local count = 0

		local data = {}
		
		for i, obj in ipairs(plot.itemHolder:GetChildren()) do
			table.insert(data, {
				["name"] = obj.Name,
				["tramsform"] = {
					["x"] = obj.PrimaryPart.CFrame.X;
					["y"] = obj.PrimaryPart.CFrame.Y;
					["z"] = obj.PrimaryPart.CFrame.Z;
					["r"] = obj.PrimaryPart.Orientation.Y
				}
			})
		end

		local success, err

		repeat
			success, err = pcall(function()
				dataStore:SetAsync(key, data)
			end)

			count = count + 1
		until count >= tries or success

		if not success then
			warn("Data could not be set." .. tostring(err))

			return
		end
	else
		warn("Data has not been loaded. Do not attempt to set data when it has not been loaded.")

		return
	end
end

local function deserialize(plr)
	local plot = plotManager.returnPlot(workspace.Plots, plr)
	print(plot)
	
	local key = plr.UserId
	local count = 0

	local data

	local success, err

	repeat
		success, err = pcall(function()
			data = dataStore:GetAsync(key)
		end)

		count = count + 1
	until count >= tries or success

	if not success then
		warn("Failed to read data." .. tostring(err))

		plr:Kick("Failed to read data. Please rejoin the game.")

		return
	end

	if data then
		local plot = plotManager.returnPlot(workspace.Plots, plr)
		print(plot)
		
		
		for i, saved in ipairs(data) do
			local loadedModel = replicatedStorage.Models:FindFirstChild(saved.name):Clone()
			
			if loadedModel then
				loadedModel:SetPrimaryPart(CFrame.new(saved.transform.x, saved.transform.y, saved.transform.z)*CFrame.Angles(0, math.rad(saved.transform.r), 0))
				
				loadedModel.Parent = plot.itemHolder
			else
				return
			end
		end
		
		dataloaded = true

		return data
	else
		dataloaded = true

		return {}
	end
end

local function unloadTycoonData(plr)
	local plot = plotManager.returnPlot(workspace.Plots, plr)
	
	serialize(plr)
	
	for _, obj in ipairs(plot.itemHolder:GetChildren()) do
		obj:Destroy()
	end
	
	plot.Owner.Value = ""
end

players.PlayerAdded:Connect(deserialize)
players.PlayerRemoving:Connect(unloadTycoonData())

game:BindToClose(function()
	for i, plr in ipairs(players:GetPlayers()) do
		serialize(plr)
	end
end)

All help appreciated! :grin:

1 Like

Hi!

I think because the players.PlayerAdded:Connect(deserialize) doesn’t call the function deserialize with the parameter of Player. The same for players.PlayerRemoving:Connect(unloadTycoonData()).

Try to change those lines to the following:

players.PlayerAdded:Connect(function(plr)
    deserialize(plr)
end)

players.PlayerRemoving:Connect(function(plr)
    unloadTycoonData(plr)
end)
1 Like

Hello PizzaArmy,
The PlayerRemoving is wrongly bound:
In the current situation you are binding it to nil because it will attempt to call unloadTycoonData() which will return nil.

Instead correct it with this line:

players.PlayerRemoving:Connect(unloadTycoonData)

Good evening.

3 Likes

Hey GameComposer,
players.PlayerAdded:Connect(deserialize) actually works, but not players.PlayerRemoving:Connect(unloadTycoonData()) because it won’t pass the function itself but the result of the function (nil in our context)

You can totally bind events by passing a function, you can use it if you want to save some lines.

Have a nice day.

2 Likes

Hey, I’m now getting:

 Tele is not a valid member of Model "Workspace.Plots.Plot1" - TeleportChar:12
  Stack Begin
  Script 'Workspace.thatguylikepizza.TeleportChar', Line 12 - TeleportChar:12
  Stack End

Hi!

Thanks for the explanation.

Have a nice day too!

Hey there, I am sorry but I cannot assist you for this part because I don’t know the code of TeleportChar.
But your issue might be related to the fact that your plot model doesn’t effectively contains a Tele part.
It is certainly a part that triggers a teleport in some way when touching it (?)

Added a WaitForChild and it worked

1 Like

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