"ServerScriptService.TycoonHandlerServer:30: attempt to index local 'Tycoon' (a nil value)"

I’m currently working on a sandbox tycoon, but I’m having trouble with one of the simplest parts. I am trying to make the system that gives the user a tycoon, but for whatever reason it returns nil. This is the code:

local Tycoons = workspace.TycoonHolder
local Event = game:GetService('ReplicatedStorage').Event
local Players = game:GetService('Players')
function GetAvalibleTycoon()
	wait(1)
	for _,v in pairs(Tycoons:GetChildren()) do
		if v then
			if v.Info.Owner.Value ~= nil then
				print(v.Name)
				return v
			end
		end
	end
end

Players.PlayerAdded:connect(function(Player)
	local Tycoon = GetAvalibleTycoon()
	wait()
	if Tycoon then
		print(3)
		local Info = Tycoon.Info
		Info.Owner.Value = Player
		print(4)
	end
end)

Any help with this?

Just a note it’s available, not Avalible. And your title says line 30? I can only see 25 lines.

Which line is it failing on? I don’t see any immediate problems with the script.

It fails on line 23. The full error is:

16:38:20.836 - ServerScriptService.TycoonHandlerServer:23: attempt to index local ‘Tycoon’ (a nil value)
16:38:20.836 - Stack Begin
16:38:20.837 - Script ‘ServerScriptService.TycoonHandlerServer’, Line 23
16:38:20.837 - Stack End

Yes, but in the code, what line? Your post doesn’t provide the line numbers.

local Tycoon = GetAvalibleTycoon()

EDIT: I took out the check to see if Tycoon is nil so I could see what is wrong. the “GetAvalibleTycoon” function returns nil for whatever reason.

Check the Explorer. Is everything set up properly? Are there tycoons in the TycoonHolder? is the Owner value blank?

I’ve already checked all that.
image

EDIT: And what do you mean by “blank”? I have the value set to nil by default.

Oh! I see what’s wrong.

if v.Info.Owner.Value ~= nil then

The ~= symbol is used to check if the statement is NOT the case. You should use == like this:

if v.Info.Owner.Value == nil then

Nevermind, figured it out! I did

if v.Info.Owner.Value ~= nil then
When I should have done

if v.Info.Owner.Value == nil then

EDIT: Lol, that was a dumb mistake.

1 Like

We’ve all made it at some point. It took me 10 minutes to notice it.

Same lol, thanks anyway man!

1 Like