Attempt to index nil with 'Data'

I need help with this error because i cant find any solutions

The tycoon module

function Tycoon.ClaimTycoon(player, tycoon)
	local starterBuild = tycoon.StarterBuild
	
	if tycoon:GetAttribute("Owner") ~= "NoOne" then return end
	
	tycoon:SetAttribute("Owner", player.Name)
	
	local claimedTycoon = Instance.new("StringValue")
	claimedTycoon.Name = "ClaimedTycoon"
	claimedTycoon.Parent = player
	
	Tycoon.LoadTycoon(player, tycoon)
	local profile = Manager.Profiles[player]
	local collectionPad = tycoon.StarterBuild.CollectingPad
	
	while task.wait(1) do
		Manager.adjustTycoonCurrency(player, profile.Data.TycoonData.TycoonCurrency)
	end
	
	collectionPad.PrimaryPart.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			local playerTouch = Players:GetPlayerFromCharacter(hit.Parent)
			
			
			if playerTouch.Name == player.Name then
				if collectDebounce[player] then return end
				collectDebounce[player] = true
				
				profile.Data.leaderstats.Cash += profile.Data.TycoonData.TycoonCurrency
				profile.Data.TycoonData.TycoonCurrency = 0
				
				task.wait(2)
				if collectDebounce[player] then
					collectDebounce[player] = false
				end
			end
		end
	end)
	
	RunService.Heartbeat:Connect(function()
		collectionPad.BillboardGui.TextLabel.Text = player.Name.." 's tycoon \ncash to collect: \n"..FormatNumberAlt.FormatStandard(profile.Data.TycoonData.TycoonCurrency)
	end)
end

i thought it was the template but it looks fine to me

local Template = {
	Cash = 0,
	TycoonData = {
		OwnedPads = {},
		RatePerSecond = 1,
		TycoonCurrency = 0
	}
}
return Template
local Manager = {}
Manager.Profiles = {}

function Manager.adjustTycoonCurrency(player: Player, amount: number)
	local profile = Manager.Profiles[player]
	if not profile then return end
	
	profile.Data.TycoonData.TycoonCurrency += amount
	print(profile.Data.TycoonData.TycoonCurrency)
end

return Manager

image

3 Likes

In short and simplified, you are trying to use something that the script doesn’t see/have defined.

Where exactly is this ‘Data’ located? Is it only available to the client or can it also be used by the server?

Moreover (although I assume that this is evident ), have you defined what “Data” is to the server script?

what.

im using Profile Service ‘Data’ is the only way to use it

nvm still not figured out :pensive:

What is on line 166?

Can we see that line of code?

And line 37.

while task.wait(1) do
	Manager.adjustTycoonCurrency(player, profile.Data.TycoonData.TycoonCurrency)
end

Try adding print statements to see what is invalid:

while task.wait(1) do
	
	print("Manager = ", Manager)
	print("Player =", Player)
	print("profile.Data.TycoonData.TycoonCurrency =", profile.Data.TycoonData.TycoonCurrency)
	
	Manager.adjustTycoonCurrency(player, profile.Data.TycoonData.TycoonCurrency)
end

it still says

ServerScriptService.PlayerData.TycoonModule:161: attempt to index nil with ‘Data’

Manager = ▼ {
[“Profiles”] = {},
[“adjustCash”] = “function”,
[“adjustTycoonCurrency”] = “function”

My guess is that TycoonData does not exists. Try adding these lines:

	print("profile =", profile)
	print("profile.Data =", profile.Data)
	print("profile.Data.TycoonData =", profile.Data.TycoonData)

it does exist but maybe im doing it wrong

image

and again here is the template which i originally thought was the problem

local Template = {
	Cash = 0,
	TycoonData = {
		OwnedPads = {},
		RatePerSecond = 1,
		TycoonCurrency = 0
	}
}

return Template
1 Like

Try checking within the module and make sure you know what it is receiving:

function Manager.adjustTycoonCurrency(player: Player, amount: number)
	
	print("player: Player, amount: number =", player, amount)
	
	local profile = Manager.Profiles[player]
	if not profile then return end

	profile.Data.TycoonData.TycoonCurrency += amount
	print(profile.Data.TycoonData.TycoonCurrency)
end

now it says the profile is nil

Where do you define profile?

line 152

local profile = Manager.Profiles[player]

Again, print each item defined on line 152 and make sure they are what you think the are.

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