So I am currently trying to make a plane spawner for my friends airline. I have a base part and a clicking part. The clicking part has a Click Detector in it, a Local Script and a Gui. I am trying to make it so that when the clicking part is clicked, the Gui will become visible to only the player that clicked it. Inside the gui is a frame that takes up the whole entire window, 3 buttons in the frame, and those buttons have scripts that simply use MouseButton1Click to clone the plane from server storage into workspace. The code for the clicking part is this:
local Clicker = script.Parent.ClickDetector Clicker.MouseClick:Connect(function() script.Parent.Main.Enabled = true end)
Any help would be appreciated!
LocalScripts
cannot run in global environments, that of which includes workspace
. They're meant to run on the Client, therefore are only functional in local environments. To solve your issue, simply transfer your code into a SeverScript
.
Due to Filtering Enabled, ServerScripts
cannot modify local Objects—or without some loopholes—one of which includes the PlayerGui
which is used to control the User Interface To gain access to the PlayerGui
we need a Player Object, we cannot use .LocalPlayer
however since ServerScripts
aren't run on local machines, so we'll need to find an alternate way to gain access to the respective Player that needs the GUI Instance enabled.
Luckily, this isn't as hard with ClickDetectors
, since they fortunately provide the Player Object as a parameter to the Client that initiated the .MouseClick
signal.
Your issues don't stop here however, as we need to solve one last problem. As aforementioned, the PlayerGui
is the container used to manage the UI, meaning all GUI Objects are required to be a descendant of this Folder, otherwise they will not not appear or function at all. To initiate a UI, store all Instances relative into StarterGui
; they will be actively transfered to the PlayerGui
upon joining.
local Clicker = script.Parent.ClickDetector Clicker.MouseClick:Connect(function(PlayerClicked) local PlayerGui = PlayerClicked:WaitForChild("PlayerGui") local Main = PlayerGui:WaitForChild("Main") Main.Enabled = true end)
First add a local script, obviously because only the client (you) can see the click detector and gui's
script:
local Clicker = script.Parent.ClickDetector Clicker.MouseClick:Connect(function() script.Parent.Main.Enabled = not script.Parent.Main.Enabled end)