How come script.Parent is nil?

Hey guys,

Today may not be a good day to code… oh gosh.

Here’s the problems

I got a ModuleScript inside a Folder which is inside ReplicatedStorage as well as a ObjectValue, here’s the tree:

ReplicatedStorage
–Folder
----ModuleScript
----ObjectValue

Inside my ModuleScript I’m trying to access the value of ObjectValue through the code below:

local ov = script.Parent.ObjectValue

The error I get is:
Attempt to index nil with ObjectValue

So I decided to print(script.Parent) and for my surprise it returns nil.

Why is it happening?

Thanks.

Maybe because when you require a module script you get a version of it that’s parented to nil? That was just a guess, I have no idea why this happened, but you can just get to the ObjectValue with a path starting with game instead of a path starting with script.

local ov = game.ReplicatedStorage.Folder.ObjectValue

This way it doesn’t matter if script’s parent is nil.

1 Like

That did the trick, so thanks for that.

I would really like to understand these type of issues… but I think your guess make sense… anybody else?

@ScriptingSausage I’ll wait a few minutes before close this thread just to get a chance to have an explanation about it.

The first reply is a good guess, but incorrect. Requiring Modulescripts runs the code in the modulescript “from” the modulescript (so using script works like it would in any other lua container), so requiring a module with this code in it that is parented under workspace will print workspace:

print(script.Parent.Name)

My guess is you have something parenting scripts to nil, or something that destroys the script. Have you checked the modulescript is still there on whatever side (server or client) you’re requiring it from? By default modulescripts don’t have this strange behavior, so its very likely something you did. If you have no code that could be destroying the modulescript, try opening a baseplate and putting a modulescript under workspace with the code above and requiring it, and it should print workspace. If it doesn’t print that, then some plugin might be destroying scripts or something.

2 Likes

Oh, that makes sense…

Could it be because I’m requiring a clone of the moduleScript then?
e.g:

local dataPreferences = require(game.ReplicatedStorage.Modules.PlayerData.preferences:Clone())

Yes, because cloned Instances are parented to nil by default.

1 Like