Hello!
So I'm making a GUI button that turns on a part (see script). When I sit in the seat, the GUI pops up (so the GUI is cloned to the PlayerGUI).
The script:
local light = script.Parent.Parent.Parent.Parent.Parent.Light script.Parent.MouseButton1Click:connect(function() light.Sound:Play() light.Material = Enum.Material.Neon light.SurfaceGui.TextLabel.Text = "Stop Request" script.Parent.Visible = false script.Parent.Parent.fake.Visible = true end)
(see link) http://prntscr.com/jy5na1
The only thing is: You know when this script has been cloned to the PlayerGUI, the "script.Parent.Parent.Parent.Parent.Parent.Light" has changed because that's not in PlayerGUI. How do I redirect that back to the Light part?
Any help would be appreciated.
Greetings,
Snewo12
I do not understand how you meant the Light
part had Changed?
Which is why this is probably not even the answer you are looking for but nevertheless, it has some useful suggestions for future. Comment on the answer below if this was not the solution you wanted. You do not need to create a new answer to post a comment but merely write your comment on the comment bar which is below every answer.
The first thing you have to do is stay organized. The reason why you are running into errors is that you are not organized.
Here are some ways in which you can improve your Model
.
Model
. What is the Model
? A car? If so, name it Car_Model
. script
as well. SurfaceGui
to what it is actually about. Sound
to what it is, i.e. Honk, Brake etc. That is pretty much the complaints I have in Models. I do, however, have a lot for the scripts.
Parent
, consider actually writing each one out as a variable. -- [Declaration Section] -- //Game Services local Workspace = game:GetService("Workspace"); -- //Model Location local Car_Model = Workspace.Car_Model; local Seat1 = Car_Model.Seat1; local Message_Gui = Seat1:WaitForChild("MessageGui"); local Light = Car_Model.Light; local Surface_Gui = Car_Model:WaitForChild("SurfaceGui"); local Sound = Light:WaitForChild("Sound");
The above method is my preferred way. Any person who looks at my code can immediately identify things without the need to add a picture to describe those Parent
s. Of course you are eligible to use Parent
s. You are using 5 Parent
in this case, which is not a good idea.
connect:
is depreciated. Consider switching over to Connect:
. It is just a capital C. It won't hurt to switch over. Now you did say rest of the stuff works so I won't do much there. You, however, have not posted what exactly you cloned. Did you clone the whole Model?, Did you clone the script?. What exactly did you clone? Put more information on that. I am going to continue on assuming you cloned Message_Gui
You said,
You know when this script has been cloned to the PlayerGUI, the "script.Parent.Parent.Parent.Parent.Parent.Light" has changed because that's not in PlayerGUI.
From my understanding, it looks like the Light
part's location breaks off.
Why?
Well you said Light = script.Parent.Parent.Parent.Parent.Parent.Light
, you declared Light
from the LocalScript
. Now when the LocalScript
is moved to the PlayerGui
, the script will not find the Light
because it will look at all the Parent
's and find that there is not Light
Part anywhere.
Solution.
Now, in cases like this, you want to declare the Light
after the script has been cloned or simply, declare it from top to bottom. Which is why, you don't want to use Parent
's in these cases.
-- [Declaration Section] -- //Game Services local Workspace = game:GetService("Workspace"); -- //Model Location local Car_Model = Workspace.Car_Model; local Seat1 = Car_Model.Seat1; local Message_Gui = Seat1:WaitForChild("MessageGui"); local Light = Car_Model.Light; local Surface_Gui = Car_Model:WaitForChild("SurfaceGui"); local STextbox = Surface_Gui:WaitForChild("TextLabel"); local Sound = Light:WaitForChild("Sound"); -- [Processing Section] -- [Your cloning stuff needs to be added here.] Gui_Stuff = function() Sound:Play(); Light.Material = Enum.Material.Neon; STextbox = "Stop Request"; Message_Gui.Enabled = false; end; -- [Connecting Section] STextbox.MouseButton1Down:Connect(Gui_Stuff);
So why will this method work in studio
Parent
was the reason you could not do the stuff inside the function. I went from game
to Workspace
. Then I went downt to Car_Model
. Then I went to Light
. Now the Message_Gui
is already cloned and in my way, the Light
has nothing to do with Message_Gui
. Thus, it works. Well I am getting bored of writing and writing now and I will close this answer with some helpful resources you can check out.
Useful Resources to Study more on.
Should you have any questions, feel free to comment below, not create a new answer. Have fun debugging.