Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

I have a question about this Button GUI. It doesn't redirect to the part?

Asked by
snewo12 72
5 years ago

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

0
How did it go? Did it work? Be sure to accept the answer so we both earn some good reputation points :) Zafirua 1348 — 5y

2 answers

Log in to vote
1
Answered by
Zafirua 1348 Badge of Merit Moderation Voter
5 years ago
Edited 5 years ago

Your explanation was not very clear. Consider describing what exactly you meant by the "because that's not in PlayerGUI. How do I redirect that back to the Light part?".

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.

  • Name the Model. What is the Model? A car? If so, name it Car_Model.
  • It is also a good practice to name the script as well.
  • Again, rename your SurfaceGui to what it is actually about.
  • Rename the 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.

  • Since you are using quite a lot of 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 Parents. Of course you are eligible to use Parents. 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);
Do not copy the above script. Simply use it as a reference because it may not work like how you desire. You will most probably also run into a problem of working only in studio. This is fixable by using Remote Events and Function which I will leave the links down below. I suggest you check them out.

So why will this method work in studio

  • I declared variables properly.
    • Using the 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.
  • I was organized.
    • The code may have more lines than before but It is readable. Now, the Computer does not care how messy you right. But since you were not able to debug properly, writing in a organized way is a must.

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.

0
Like I said before, you know what a bus has a stop button? I want to make that on roblox by a GUI. Since buttons are not easy to click while driving. Anyways, at "Why?" (what you said), the location of the Light part indeed break off (I said redirecting the whole time while I meant location, sorry I'm dutch xd). 1/4 snewo12 72 — 5y
0
Also, about that cloning, I meant the GUI that you get from the seat cloned to the PlayerGUI. Not that a part has been cloned :P. I was doing variables at first, but it still didn't work. So I thought let me just write it out, hence those parents. 2/4 snewo12 72 — 5y
0
About that model naming, I didn't knew that was possible in a script :O And yes, I'll be renaming default things from now on. 3/4 snewo12 72 — 5y
0
But I see it's kinda complicated to locate the "Light" part again, as I see in your script. Anyways, thanks for info and for the links. I'll look again, if I need more help, I'll contact you. Thanks again. Also, I'm sorry about that commenting in an answer bar. 4/4 snewo12 72 — 5y
Ad
Log in to vote
0
Answered by
vkax 85
5 years ago

I don't get what you mean, can you please elaborate more clearly?

0
Well, you know that a bus irl has a stop button? I want to make that but a stop GUI button. snewo12 72 — 5y

Answer this question