Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Nil value even though variable is already described?

Hiya! In the script i have, I was trying to make an owner's only door. only problem is... when my script describes the variable of tycoon on line 14, on line 15 the script tells me it has a nil value. Help?

dad = script.Parent
door = dad.Parent
owner = door.Parent.Owner

local active = 0

function onTouched(part)
	local human = part.Parent:findFirstChild("Humanoid")
	if human ~= nil and active == 0 then
		active = 1
		local player = game.Players:findFirstChild(part.Parent.Name)
		if player ~= nil then
			if owner.Value == "" then
				local tycoon = player:findFirstChild("Tycoon") -- describes tycoon
				if tycoon.Value == 0 then -- where error eccours
					tycoon.Value = 1
					owner.Value = player.Name
					local message = Instance.new("Message")
					message.Text = "You now own this tycoon!"
					message.Parent = player
					wait(3)
					door.Name = "Tycoon Owner: " .. owner.Value
					message:remove()
				else
					local message = Instance.new("Message")
					message.Text = "You already own another tycoon!"
					message.Parent = player
					wait(3)
					message:remove()
				end
			end
		end
	end
	active = 0
end

dad.Touched:connect(onTouched)

Here's what the output says.

10:00:49.307 - Workspace.MrNiceGuy's Beginner Tycoon Kit.Tycoon.This Tycoon has no owner..OwnerDoor.[MNG]-OwnerScript:15: attempt to index local 'tycoon' (a nil value)

2 comments

0

your using "findfirstchild" which doesn't guarentee it will return an object, use "waitforchild" instead

0

also its "FindFirstChild" not "findFirstChild"

1 answer

maybe because the player isn't apart of the tycoon yet so instead do waitforchild so when it is there then maybe it'll work or maybe the player is already in the tycoon then idk

This discussion is preserved as it was. New answers, comments, and votes are closed.