Beginner Question about Selection in Scripts

I just have a simple question here regarding item selection.

Im very new to lua, and have learned to select something in a script to do as follows:

game.Workspace.part -- etc

But I also see this:

script.Parent -- etc

I was wondering what the difference/uses of these two types of scripts are for? because in theory to me and videos I have seen they seem pretty similar and couldn’t find what the difference between the two are.

Thanks!

game.Workspace.Part is an absolute path to the part in the workspace. game is at the very top of the hierarchy, so you’re starting from the top to wherever the part is.

script.Parent is a relative path since the path is relative to the script. You should use this if the script is simply in the part.

3 Likes

I usually use game.Workspace.part when scripting in like ServerScriptService, StarterGui, etc. and grabbing it from a script in there.

script.Parent would be if you put a script in something else (for example a script in a part in Workspace, etc).

You can still use game.Workspace.part when using a script in the part, but script.Parent is just easier for me.

1 Like

Selection should be quite easy to manipulate.

script.Parent isn’t recommended sometimes. Because of it, it may print out a different selection than the original. Imagine this. If the script was originally parented to ServerScriptService then suddenly parented to Workspace. script.Parent wouldn’t be the same anymore.

However, adding script.Parent to a variable would much help.

local onparent = script.Parent

It would most likely help if you are retrieving items. So like if the script.Parent had no TestExample when you were doing script.Parent:FindFirstChild("TestExample") or script.Parent.TestExample, it result in a nil because it wasn’t the same parent before and that original parent have TestExample.

It’s better having onparent if the script just gets parented to different places.


Instance:WaitForChild(“AnotherInstance”)

If you are retrieving an instance, for example, another instance would be AnotherInstance. This method is recommended if you are wanting to retrieve an instance with its name “AnotherInstance” for example only. When a new child is added and it is named after “AnotherInstance”, it would select that object. Otherwise, it would keep on waiting til it finds the child with the right name.

Both mean the same thing as long as the script is inside of the Part. For me, I prefer the game.Workspace.Part just because it’s easier for me to understand. Others might use script.Parent because it’s shorter, but in my opinion I think the other way is simpler and self-explanatory.

Why are you randomly changing the Parents of scripts in your game at run-time?? That sounds like a recipe for disaster to begin with.

3 Likes

follow up question if you don’t mind.

I’m following some tutorials and I’m learning about events right now. this is my current script:

function transparent()
	game.Workspace.Part.Transparency = 0.5
end

game.Workspace.Part.Touched:Connect(transparent)

the command works, when its touched the command runs, because I tested with a print output, but for some reason the function is getting triggered before my player even touches the part. I’m very confused as to why this is happening considering its in a function that has not been called yet

I may be wrong but maybe it’s doing that because the part is touching the baseplate?

1 Like

Wow I never would have guessed that, Thank you for the help!

1 Like

I didn’t think that would be the solution but your welcome.

You would have to make something where if the Humanoid touches it (Humanoid is just a simple part of the Character.) then it will trigger it. Can’t quite recall how but it would be fairly simple I believe. If you know what variables are, which I hope you do, then you will know that you need a variable for the location of ‘Humanoid’. Then you will probably need a thing where it WaitsForChild/Character/Humanoid to make sure your script doesn’t give an error.

Sorry that I can’t help fully. But maybe ask in another topic how to find get the location of a Humanoid.