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

Click Detector .MouseClick not working?

Asked by 4 years ago

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!

1
you have to parent the gui in the players playergui in order for it to work. greatneil80 2647 — 4y
0
Ensure you put "Main" under StarterGui Ziffixture 6913 — 4y

2 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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.

However...

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)
0
bad feahren. greatneil80 2647 — 4y
0
Lol, been a while greatneil;) Ziffixture 6913 — 4y
0
thats what i said in the community chat but u wont RESPOOOONDDDDDDDDDddddddddddddddddd greatneil80 2647 — 4y
0
The script you provided doesn't work for me during testing in studio. Also when I pull up explorer during testing, the player does not have Main in PlayerGui. electrocat56 18 — 4y
Ad
Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago

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)

Answer this question