Else not working?

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)

Does changing player.PlayerGui:FindFirstChild("GUI") make a difference?

1 Like

Try using this

 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)

Did not work, still giving me the same error “Gui not found”

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.

Use this instead:

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)

Just to fix up @goldenstein64 's solution a bit…

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.

1 Like

can you give us the locations of the things?

It looks like the GUI is a child of the script? Was that a mistake?

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()