So I have a script that gives a gui, and sometimes the gui is given twice which I do not want, so I made this, but now it does not work at all. Any help?
script.Parent.GiveGui.OnServerEvent:Connect(function(player,Prisoner)
if player.PlayerGui.GUI then
player.PlayerGui.GUI:Destroy()
local Gui = script.GUI:Clone()
Gui.Parent = player.PlayerGui
Gui.Menu.SentenceMenu.Sentence.MouseButton1Click:Connect(function()
local PrisonerPlayer = game.Players:GetPlayerFromCharacter(Prisoner)
Cuff(PrisonerPlayer,Gui)
end)
else
local Gui = script.GUI:Clone()
Gui.Parent = player.PlayerGui
Gui.Menu.SentenceMenu.Sentence.MouseButton1Click:Connect(function()
local PrisonerPlayer = game.Players:GetPlayerFromCharacter(Prisoner)
Cuff(PrisonerPlayer,Gui)
end)
end
end)
script.Parent.GiveGui.OnServerEvent:Connect(function(player,Prisoner)
if player.PlayerGui.GUI then
player.PlayerGui.GUI:Destroy()
local Gui = script.GUI:Clone()
Gui.Parent = player.PlayerGui
Gui.Menu.SentenceMenu.Sentence.MouseButton1Click:Connect(function()
local PrisonerPlayer = game.Players:GetPlayerFromCharacter(Prisoner)
Cuff(PrisonerPlayer,Gui)
end)
else
player.PlayerGui.GUI:Destroy() -- Since both scripts are the same (the if and the else) why aren't both scripts that come from that the same? I added this because of that.
local Gui = script.GUI:Clone()
Gui.Parent = player.PlayerGui
Gui.Menu.SentenceMenu.Sentence.MouseButton1Click:Connect(function()
local PrisonerPlayer = game.Players:GetPlayerFromCharacter(Prisoner)
Cuff(PrisonerPlayer,Gui)
end)
end
end)
I simplified your script a little bit so you don’t have to include cloning the gui in both conditionals:
script.Parent.GiveGui.OnServerEvent:Connect(function(player,Prisoner)
if player.PlayerGui:FindFirstChild("GUI") then
player.PlayerGui.GUI:Destroy()
end
local Gui = script.GUI:Clone()
Gui.Parent = player.PlayerGui
Gui.Menu.SentenceMenu.Sentence.MouseButton1Click:Connect(function()
local PrisonerPlayer = game.Players:GetPlayerFromCharacter(Prisoner)
Cuff(PrisonerPlayer,Gui)
end)
end)
You could do 2 things:
Create a while or repeat loop that destroys gui’s named GUI until they don’t exist:
--
local gui = player.PlayerGui:FindFirstChild("GUI")
while gui do
gui:Destroy()
gui = player.PlayerGui:FindFirstChild("GUI")
end
--
Look through all children instead:
--
for _, gui in ipairs(player.PlayerGui:GetChildren()) do
if gui.Name == "GUI" then
gui:Destroy()
end
end
--
You could also just do all this from a local script instead. Like instead of firing a remote event to get a gui from the server, you could clone a gui from replicated storage all from the client and just fire a remote event from GUI.Menu...MouseButton1Click:Connect on the client instead so that they call Cuff. It would be just as secure, given the client is defining the Prisoner in the first place.
script.Parent.GiveGui.OnServerEvent:Connect(function(player,Prisoner)
if player.PlayerGui:FindFirstChild("GUI") then
player.PlayerGui.GUI:Destroy()
end
local Gui = script.GUI:Clone()
Gui.Parent = player.PlayerGui
Gui.Menu.SentenceMenu.Sentence.MouseButton1Click:Connect(function()
local PrisonerPlayer = game.Players:GetPlayerFromCharacter(Prisoner)
Cuff(PrisonerPlayer,Gui)
end)
end)
By adding :FindFirstChild, you’re actually checking for the GUI. By doing PlayerGui.GUI, it can cause an error if GUI isn’t a child of PlayerGui, because you’re telling the script it’s there when It might not be.
Wait, frst off, "local PrisonerPlayer = game.Players:GetPlayerFromCharacter(Prisoner), erm… Prisoner variables I’ll guess is something like, game.Workspace:FindFirstChild(Prisoner.Name) right, Also you can’t use a function inside a variables which is cloned I’ll guess, at this point: Gui.Menu.SentenceMenu.Sentence.MouseButton1Click:Connect(function()