Do I use a LocalScript or a Script for PlayerGUI?

Hello! So I have a GUI and I want to make another one but I would like to make sure I am doing it correctly.

To activate the GUI, I use a Click Detector with a regular Script inside. Inside the GUI, I use Local Scripts, but I use a Script to close it. Is this a good way to do this? (I am opening/closing the GUI by using the enabled property) I am not sure if I am doing it right.

2 Likes

You should use a LocalScript for all GUI interactions. Do not use a Script for anything except where you need server interaction, it is bad practice! (It might work, but it would work poorly and will be very unresponsive)

4 Likes

The problem is, when I replace my script with a LocalScript the ClickDetector doesn’t work. Someone said on a DevForum article you shouldn’t use a LocalScript with a ClickDetector, so how would this work?

1 Like

It appears like you do need server interaction, therefore using a script to write the clickdetector is perfectly fine. I’m guessing you’re allowing the players to kick off a GUI by interacting with an object by simply clicking on it… sure that’s fine. Send over a remote event to the client that clicked on the object to enable the GUI.

2 Likes

cc @tragoblue

ClickDetectors can be done completely on the client-side. As there are no server-side interactions here (it is only opening a Gui based on if a ClickDetector is clicked), no scripts should be involved here. Use only LocalScripts. RemoteEvents will incur unnecessary network overhead. :slight_smile:

If the code does not work from a LocalScript, it’s an implementation error and it would help to see the code you are working with so that we can offer advice on how you can clean up your code to make it work properly.

2 Likes

Here is my script:

gui = game.StarterGui.Folder.GUI.Enabled == false

script.Parent.ClickDetector.MouseClick:Connect(function(player)
player:WaitForChild(“PlayerGui”).Folder.GUI.Enabled = true
while true do
wait(.5)
end
end)

  • It’s in a Script under the ClickPart

Yeah, that’s the problem, LocalScripts do not run in the Workspace. It’s always a good idea to read documentation first in case you are unsure of why something is the way it is so you can have at least a bit of understanding or material to go by when asking questions. Documentation on LocalScripts.

The LocalScript should be moved preferably into PlayerScripts. Then, instead of script.Parent you would be making a reference to the click part. If this click part is deeply rooted in the hierarchy (which interactable objects shouldn’t be unless you have an easy way to access them), you can use an ObjectValue as a temporary reference.

I’m not sure what the purpose of either the StarterGui access or the while loop are, but those ideally can and should be removed as they don’t serve any purpose.

3 Likes

Run it under a Player property - running a LocalScript in the Workspace won’t work because it’s not running under a client (hence why it’s a LocalScript and not just Script as it runs locally).

1 Like

Always use LocalScript for UI.

3 Likes

Thank you so much for leading me in the right direction! It now functions properly :slight_smile:

If your problem has been solved, mark the reply that helped you as the Solution.

1 Like

Thanks for reminding me, I forgot to :grimacing:

Okay, I understand the issue now! Thanks for helping to clear out my confusion.

1 Like