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

Error with FindFirstChild()?

When I run the script below I get this error

  • 20:13:07.784 - Players.Player1.PlayerGui.Menu.Home_Frame.Lock/Unlock.ButtonScript:10: attempt to index upvalue 'dor' (a nil value)

20:13:07.784 - Stack Begin

20:13:07.785 - Script 'Players.Player1.PlayerGui.Menu.Home_Frame.Lock/Unlock.ButtonScript', Line 10

20:13:07.785 - Stack End *

local player = game.Players.LocalPlayer
local playerpref = game.Workspace.PlayerProfiles:FindFirstChild(player.Name)
local lockval = playerpref:FindFirstChild("Locked")
local house = game.Workspace.PlayerProfiles:FindFirstChild(player.Name):FindFirstChild("HouseOwned"):FindFirstChild("HouseNumber").Value
local dor = game.Workspace:FindFirstChild(house):FindFirstChild("Door")

script.Parent.MouseButton1Down:connect(function(clicked)
	if lockval.Value == false then
		lockval.Value = true
		dor.CanCollide = true
		script.Parent.Text = "unlock"
	else
		lockval.Value = false
		script.Parent.Text = "lock"
		dor.CanCollide = false
	end
end)

1 answer

✓ Accepted answer

The error message

First off, whenever you get an error message, it's very important to analyze it as best you can. It's pretty much an answer to what the problem is, without the explanation. Any error message that says anything along the lines of attempt to index <something> (a nil value), it just means that your script tried to do something on a value that doesn't exist. nil = non-existent

Breaking down the problem

In this case it's actually quite simple. Since our error message is telling us that dor is nil, all we have to do is find out why.

FindFirstChild is a function that will look for an object that's inside of the object the method was called on based on it's name. However if it doesn't find the matching object's name, it will return nil.

Solution

So retaining all of this, we can conclude that the problem stems from your FindFirstChild function for the following reasons:

1: FindFirstChild must have a string value as it's argument that represents the name of the object you're looking for. Anything else, will not work.

2: It's also possible FindFirstChild could return nil if the object doesn't exist once you've corrected the mistake explained above, but this all depends on the environmental conditions of your game, in which case you could physically change with ease.

Hope this helped, let me know if you have any questions.

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