Attempt to index nil with 'x'

I am getting this error when TycoonDataManager attempts to load the saved objects:

  ServerScriptService.TycoonDataManager:90: attempt to index nil with 'x' - TycoonDataManager:90
  Stack Begin
  Script 'ServerScriptService.TycoonDataManager', Line 90 - function deserialize - TycoonDataManager:90
  Script 'ServerScriptService.TycoonDataManager', Line 121 - TycoonDataManager:121
  Stack End

TycoonDataManager:

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(function(plr)
	deserialize(plr)
end)

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

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

If anyone can help me fix this it will be appreciated!

1 Like

Hi. Can you quickly show me line 90? That’s where the error is happening.

1 Like

Line 90

loadedModel:SetPrimaryPart(CFrame.new(saved.transform.x, saved.transform.y, saved.transform.z)*CFrame.Angles(0, math.rad(saved.transform.r), 0))
1 Like

Try changing this to “transform” and see if that changes anything. If not, let us know.

You named your saved value “tramsform” but you’re trying to index “transform”.

Same error. Also I’d like to note the tutorial is over 2 years old

  ServerScriptService.TycoonDataManager:90: attempt to index nil with 'x' - TycoonDataManager:90
  Stack Begin
  Script 'ServerScriptService.TycoonDataManager', Line 90 - function deserialize - TycoonDataManager:90
  Script 'ServerScriptService.TycoonDataManager', Line 121 - TycoonDataManager:121
  Stack End

Well, fixing the name of the saved value is only going to change what gets saved from now on, you still have a datastore full of stored tables with a “tramsform” entry. You have to check saved data for both, by trying to read both and checking for nil before trying to access x,y,z, and r. Then, resave the user’s data with the key name fixed.

If you can possibly provide edited code that would be great, I’m not the best programmer and this is all following a tutorial.

try this instead

loadedModel:SetPrimaryPart(CFrame.new(saved.tramsform.x, saved.tramsform.y, saved.tramsform.z)*CFrame.Angles(0, math.rad(saved.tramsform.r), 0))
SetPrimaryPart is not a valid member of Model "Part" - TycoonDataManager:90
  Stack Begin
  Script 'ServerScriptService.TycoonDataManager', Line 90 - function deserialize - TycoonDataManager:90
  Script 'ServerScriptService.TycoonDataManager', Line 121 - TycoonDataManager:121
  Stack End

(you could also SetPrimaryPartCFrame but thats deprecated)

loadedModel:PivotTo(CFrame.new(saved.tramsform.x, saved.tramsform.y, saved.tramsform.z)*CFrame.Angles(0, math.rad(saved.tramsform.r), 0))
1 Like

It worked 1 time but after reloading into the game I get

 ServerScriptService.TycoonDataManager:90: attempt to index nil with 'x' - TycoonDataManager:90
  Stack Begin
  Script 'ServerScriptService.TycoonDataManager', Line 90 - function deserialize - TycoonDataManager:90
  Script 'ServerScriptService.TycoonDataManager', Line 121 - TycoonDataManager:121
  Stack End

put this

print(saved)

before you PivotTo

then send results from output here (make sure to expand the table if its collapsed)

table: 0xbf3698f1b06f0f55

what does this mean

but inside roblox studio not roblox player :thinking:

So I run it inside roblox player?

run in roblox studio and this is what i get after printing table in roblox studio
Zrzut ekranu (38)

if that still doesnt work just

table.foreach(saved,print)

By printing table do you mean printing 0xbf3698f1b06f0f55?

Also table.foreach is deprecated

i dont know why do you get memory address instead of what i got in my roblox studio output (as i show on my screenshot)

table.foreach should print every each value inside that table if you did something like this table.foreach(table,print) this will print every each function inside table so like table.freeze, table.clear etc

in your case those are supposed to be values like string and cframe inside of transform

in short terms put this
table.foreach(saved,print)
because im trying to figure out what is inside of “saved” table

This is what i get from table.foreach

transform table: 0x113e5321f7bf26a9