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

I'm trying to make you not ABLE to spam click an NPC to open a UI. Help?

Asked by
Aqu_ia 39
4 years ago
Edited 4 years ago

Here is my script to clone the ui. It is SUPPOSED to check whether or not the UI is in PlayerGui and will clone it if it is not.

script.Parent.UpperTorso.ClickDetector.MouseClick:Connect(function(plr)
    if not plr.PlayerGui:FindFirstChild("NPC") then
    local clone = script.Parent.NPC:Clone() ----{0.175, 0},{0.185, 0}
    clone.Parent = plr.PlayerGui
    clone.MainFrame:TweenSize(UDim2.new(0.649,0,0.63,0, "Out", "Quad", 0.5))
    wait(1)
    clone.MainFrame.Body.Visible = true
    clone.MainFrame.MainFrame2.Visible = true
        end
    end)

GUI Script:

--// Variables

local NPCUI = script.Parent
local MainFrame = NPCUI.MainFrame
local Body = MainFrame.Body
local MainFrame2 = MainFrame.MainFrame2


--// Functions

MainFrame2.Exit.MouseButton1Click:Connect(function()
    MainFrame.Body:Destroy()
    MainFrame.MainFrame2:Destroy()
    MainFrame:TweenSize(UDim2.new(0.016,0,0.63,"Out","Quad",0.5))--{0.649, 0},{0.63, 0}
    wait(1)
    NPCUI:Destroy()
end)




--// Made by Aquawoman

I need some help, please. If this is not enough info, you can comment down in the question. Thank you very much.

0
What happens when you test this script, and how does it differ from what you want to happen? gskw 1046 — 4y
0
The first time I click on the NPC it works, however, when I click it the second time, the GUI doesn't appear. Aqu_ia 39 — 4y
0
this is because you are not removing the gui from the player gui Gameplayer365247v2 1055 — 4y
0
If you want the NPC's UI to appear again only after you've closed it first, you should use :Destroy() on plr.PlayerGui.NPC when the UI is closed. In that case the culprit might not be the script you've posted. gskw 1046 — 4y
View all comments (6 more)
1
I see. I have just edited the question aka the script from above to help you out. Aqu_ia 39 — 4y
0
Thanks! Could you also tell us if the ClickDetector script is a LocalScript or a regular Script, and the same about the GUI script? gskw 1046 — 4y
0
no this is actually not needed since it is a clickdetector, the clickdetector gets a signal when its clicked on which player clicked it and can then access their playergui and other things Gameplayer365247v2 1055 — 4y
0
My script for the click detector to work is just on a regular script, not a local script. For the GUI, it is a local script. Aqu_ia 39 — 4y
0
good thats how it should be but i just realised why it wont show up and i will put it down as an answer Gameplayer365247v2 1055 — 4y
0
Nevermind. Aqu_ia 39 — 4y

1 answer

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago
--// Variables

local NPCUI = script.Parent
local MainFrame = NPCUI.MainFrame
local Body = MainFrame.Body
local MainFrame2 = MainFrame.MainFrame2


--// Functions

MainFrame2.Exit.MouseButton1Click:Connect(function()
    MainFrame.Body:Destroy()
    MainFrame.MainFrame2:Destroy()
    MainFrame:TweenSize(UDim2.new(0.016,0,0.63,"Out","Quad",0.5))--{0.649, 0},{0.63, 0}
    wait(1)
game.ReplicatedStorage.DestroyGui:FireServer(NPCUI)--make a remote event and call it DestroyGui
end)




--// Made by Aquawoman

the code below will be a server script

game.ReplicatedStorage.DestroyGui.OnServerEvent:Connect(function(player,ui)
if ui.Parent = player.PlayerGui then
ui:Destroy()
end
end)

the reason we need a server script for this and a remote event is that in your local script you are just deleting the ui localy not globaly this means the server will still see it as the gui is in your player gui and since the clickdetector script is a server script it will find that gui and therefore not give you a new gui

the clickdetector script edited below

allowed = true
script.Parent.UpperTorso.ClickDetector.MouseClick:Connect(function(plr)
    if allowed then
    allowed = false
    if not plr.PlayerGui:FindFirstChild("NPC") then
    local clone = script.Parent.NPC:Clone() ----{0.175, 0},{0.185, 0}
    clone.Parent = plr.PlayerGui
    clone.MainFrame:TweenSize(UDim2.new(0.649,0,0.63,0, "Out", "Quad", 0.5))
    wait(1)
    clone.MainFrame.Body.Visible = true
    clone.MainFrame.MainFrame2.Visible = true
    wait(amount of time before the player can do it again here)
    allowed = true
          end
        end
    end)

by doing this we are setting a block that checks if something is true or not and if it is then the player can get the gui. you can use anything you want instead of allowed as long as it = true or = false and if you do = false then do the opposite way of how i did it and in the if statement do if not allowed then

0
Okay. I see now. I'll test it out and see if it works. Aqu_ia 39 — 4y
0
It works, but that leads me back to my first question which is that I'm trying to make it so the click detector isn't spammable which allows the GUI to clone itself when the click detector is clicked MULTIPLE times. Aqu_ia 39 — 4y
0
FilteringEnabled can sometimes have unexpected consequences. I would personally prefer to not clone the GUI on the server at all, and instead fire a RemoteEvent to the client to create the GUI. gskw 1046 — 4y
0
no this is not very good to do, my answer is better Gameplayer365247v2 1055 — 4y
View all comments (4 more)
0
Additionally, you need to put a check in OnServerEvent to see if `ui` is actually a GuiObject and if it's a descendant of the Player. Otherwise the RemoteEvent effectively functions as an FE bypass for deleting anything in the game. gskw 1046 — 4y
1
Here's my problem. It is spammable although I already put an "if not" statement to prevent it from being spammed. | Here's an example: https://gyazo.com/5601d7cc42f7fdead76d290377f86791 Aqu_ia 39 — 4y
0
yes gskw that is correct and i had forgotten about that but i edited it a little bit and here is the final outcome Gameplayer365247v2 1055 — 4y
0
Okay, thanks for the help. It works and it doesn't spam. Thanks again! Aqu_ia 39 — 4y
Ad

Answer this question