ClickDetector needing a double click to open a gui a second time

  1. What do you want to achieve?
    I am trying to open a gui when I click on a part. I have a gui frame and a close button.

  2. What is the issue? Include screenshots / videos if possible!
    Everything works and there are no errors, except once I close the gui, after the first time, it takes two clicks on the part to reopen it. Edit: If I keep clicking the part, it opens and closes with each click.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I couldn’t find anything specifically related to this issue on the Hub, however I have rewritten the code a handful of times trying different methods. This is the best I’ve gotten so far. I’m relatively new to scripting so please forgive my naivety.

I have a ClickDetector inside of a part and a script (not local) inside the same part. The script is:

script.Parent.ClickDetector.MouseClick:Connect(function(player)
    player.PlayerGui.CollectGui.CollectFrame.Visible = not player.PlayerGui.CollectGui.CollectFrame.Visible
end)

I also have a close button on the CollectFrame with a local script attached to it. it’s script is:

    local button = script.Parent

        local function onActivated()

        button.parent.Visible = false

    end

    button.Activated:Connect(onActivated)

I’m learning so any and all advice is welcomed, but please explain your response so I can understand, what I did wrong.

4 Likes

Is this a client-sided script or a server-sided script?

I think it’s client side but I’m not completely sure to be honest with you.

if it’s a client sided script, put this code inside the function in:

if player == game.Players.LocalPlayer then
    --That code
end
1 Like

A client-sided script has a player icon on it, a server-sided is just the paper.

so the ClickDetector script is server scripted the close button is client

Wait do you want them to be able to open it any where or in a set place?

I want it to be opened from a specific location.

I see what the issue is,
So what you’re doing when closing you close it client-sided the following happens:

  • Server thinks: It’s still enabled
  • Your Client thinks: It’s disabled

So when you use the server script
The server disables it

You either gotta do this both Client-sided or Server-sided, I suggest doing it Client-sided and putting the code inside the function in:

if player == game.Players.LocalPlayer then
    --That code
end
1 Like

I’ll try that. To be clear, If I put that addition

if player == game.Players.LocalPlayer then
–That code
end

in the server script in the part with the click detector, does that make it a client sided event?

So what the script would become:

script.Parent.ClickDetector.MouseClick:Connect(function(player)
    if player == game.Players.LocalPlayer then
        player.PlayerGui.CollectGui.CollectFrame.Visible = not player.PlayerGui.CollectGui.CollectFrame.Visible
    end
end)

No, just copy and paste the code from the server script into a new client script and put it in the same path (under the same parent) as the old server script.
And add that code with the if statement only to the click detector as you don’t need to check it for the gui.
Note, the if statement should reduce just a little bit of lag;

Quick recap:

  • Move the code from the server (click detector) script script to a new client script
  • Give the client script the same parent as the server script
  • Put the if statements around it to reduce lag (optional)

so I tried that and not the gui does not appear. To clarify it does not appear as a local script, even without the if statement.

Wait could you send the code real quick and saying above it if it’s a client script or a server script.
Just so I don’t fall behind on what you’re doing.

Current Server Script

script.Parent.ClickDetector.MouseClick:Connect(function(player)
    player.PlayerGui.CollectGui.CraftFrame.Visible = not player.PlayerGui.CollectGui.CraftFrame.Visible
end)

Current Local Script

local button = script.Parent
local function onActivated()
    button.parent.Visible = false
end

button.Activated:Connect(onActivated)

The server script is in the part
the local script is in the guibutton in the gui in PlayerGui

Edit:I don’t think I need the not visible part in the server script, I could just set it to visible = true but it only opens the gui 1 time

Would a remote event help. I understand what you said about the client thinking it’s closed, and the server thinking its open. I’m not sure how to fix that without a remote event then.

Okay so,

  1. Make the Current Server Script into a Local Script
    1.1. Make a new Local Script, give the script the same Parent as the Server Script,
    1.2. Selecting all the code in the Server Script (by pressing: CTRL + A or Command + A, on mac if I’m right!) Then copy (CTRL + C or Command + C) all the code from the Server Script and Paste it into the Client Script (CTRL + V or Command + V)
  2. Add the if statement to the newly-made Local Script (Optional)

(I don’t wanna seem rude, but I explained it very carefully so you might understand it better. :smile:)

Not at all seems rude. I tried that but then the gui did not open at all with the current script as written or with the addition of the if statement.

I think that’s why I put it into a server script

Try removing the if statement, I am kinda getting lost myself :sweat_smile: as it’s easier for me to understand things visually than with words (not sure how I am a scripter, because of that.

theres no using the else function?