Help with referring to a far away part

Hello everyone! I have a big question. How do I refer to a far away part?

Lets say I have a part in the workspace with a click detector in it. Then I also have a screen gui with a text label inside of the starter gui. Now when I click the click doctor it will make the text label visible. But I don’t know how to refer to it that far.

if game.Worspace.Part:FindFirstChild("Click Detector").MouseClick:Connect(function()
    script.Parent.TextLabel.Visible = true
end)

heres the issue I believe. Im using the term FindFirstChild wrong I think. Is there a way to make this work?

Thanks,
File

1 Like

Hello!
I would like to understand what do you mean with a “far away part”? Are you talking about a part that is physically far away from the player?

There are several issues with your code, I will write them down here.

  • Unless you manually named the ClickDetector, “Click Detector” will not be a child of your part.
  • I assume this is a LocalScript. Otherwise, you are trying to modify a GUI object using a Server script, which is a bad practice.
  • You are first looking for the child named Click Detector using a conditional statement. By immediately after connecting a function to its “MouseClick” event, it becomes useless. You should first make sure the Child of the given name exists, and, if and only if it does, connect the function!
1 Like

hi. So well I’m not actually referring to like a object that is far away. Sorry if I confused you! Like in the explorer.

how do I refer to the click detector from the script in the screen gui?

Also im not sure I understand the problems with my script. Can you help me understand it?

Also I’m using a server script since I want it to affect the whole server.

I think what you are trying to do is

local ClickDetector = script.Parent.ClickDetector

referencing paths of Instances is one of the first thing you learn in scripting
so I highly recommend you watch this beginner series by AlvinBlox

In this code provided there are multiple things wrong

  • first you are hooking an event inside an if statement which doesn’t make sense
  • second there is no Instance called “Click Detector” in part
  • third as @Salinas23 said you should be changing the UI on the client using Remote Events, but doing it on the server will work

the script is inside of the screen gui. Not the part. The script the part is another script.

I see. Referencing the Instance would then be

local ClickDetector = workspace.Part.ClickDetector

Also you should fix the things I mentioned in the earlier post

Well, the server does not even need access to a player’s Gui. You can use a LocalScript inside the Part if you want, and every time a player clicks the part, their gui comes up.

okay. Is there a way to do that?

Yes indeed!

Instead of using a regular script, use a local script instead.

okay! What should the script be inside of it?

I believe looking from the other posts:

local ClickDetector = workspace.Part:WaitForChild("ClickDetector")

ClickDetector.MouseClick:Connect(function()
    script.Parent.TextLabel.Visible = true
end)

Also a slight explanation, but the reason why a Server Script won’t work is because the changes are actually being made across the server-side, and not the client side (Or what you see on your screen) If you change it to a LocalScript, you should be all set

okay! This works thank you! Also thanks for the explanation!