New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Still have questions? Join our Discord server and get real time help.
2

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

AquxDev 41
3 months ago
Edited 3 months 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
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()
wait(1)
NPCUI:Destroy()
end)



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 590 — 3mo
0
The first time I click on the NPC it works, however, when I click it the second time, the GUI doesn't appear. AquxDev 41 — 3mo
0
this is because you are not removing the gui from the player gui Gameplayer365247v2 727 — 3mo
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 590 — 3mo
1
I see. I have just edited the question aka the script from above to help you out. AquxDev 41 — 3mo
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 590 — 3mo
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 727 — 3mo
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. AquxDev 41 — 3mo
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 727 — 3mo
0
Nevermind. AquxDev 41 — 3mo

2
Edited 3 months 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()
wait(1)
game.ReplicatedStorage.DestroyGui:FireServer(NPCUI)--make a remote event and call it DestroyGui
end)



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
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. AquxDev 41 — 3mo
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. AquxDev 41 — 3mo
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 590 — 3mo
0
no this is not very good to do, my answer is better Gameplayer365247v2 727 — 3mo
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 590 — 3mo