Why is this UI not cloning even though it has been 'destroyed'

In case you have misinterpreted the title, here’s a short clip of my problem.

Video and other references

ezgif-1-a39e0bba8d3a
44%20PM

From the video, when I first click the NPC, the UI will show up, however, the second, third, fourth, etc, the ClickDetector script in the NPC will tell me that the UI is still in the PlayerGui when it has already been destroyed.

Scripts

Click Detector Script

        local selecttion = script.Parent.SelectionBox
        local detect = script.Parent.ClickDetector
        local dialogue = game.ReplicatedStorage.dialogueGui
        local dialogues = game.Workspace.NPC["Chef Eggy"]["NPC dialogue"]

    detect.MouseHoverEnter:Connect(function()
    	selecttion.Visible = true
    end)
    detect.MouseHoverLeave:Connect(function()
    	selecttion.Visible = false
    end)
    detect.MouseClick:Connect(function(player)
    	
    if player.PlayerGui:FindFirstChild("dialogueGui") then
    				print("close the gui!")
    	end
    		if not player.PlayerGui:FindFirstChild("dialogueGui") then
    	dialogue:Clone().Parent = player.PlayerGui
    	local cloned = player.PlayerGui.dialogueGui
    	player.Character.Humanoid.WalkSpeed = 0
    	game.ReplicatedStorage.noShift:FireClient(player)
    	cloned.npcName.Text = "Chef Eggy"
    		for i = 1, #dialogues.String1.Value do
    				cloned.ImageLabel.NPCDialogue.Text = string.sub(dialogues.String1.Value,1,i)
    				wait(0.1)
    		end
    	end
    end)

GUI Script

    local cont = script.Parent
    local amountOfStrings = script.Parent.Parent.ImageLabel.Strings
    local npcName = script.Parent.Parent.npcName
    local strings = {}
    local stringAmount 
    local nextstring 
    local player = game.Players.LocalPlayer

    function search()
    for i,v in pairs(workspace.NPC:GetChildren()) do
    	if v.Name == npcName.Text then
    		local npcfile = v:FindFirstChild("NPC dialogue")
    		for i,t in pairs(npcfile:GetChildren()) do
    			if t:IsA("StringValue") and t.Name ~= "String1" then
    		table.insert(strings,t.Value)
    		print(unpack(strings))
    				nextstring = table.concat(strings)
    			else
    				print("No More Strings Left")
    		end

    		end
    	end
    end
    end

    cont.MouseButton1Click:Connect(function()
    	if cont.Text == "Close" then
    		player.PlayerGui.dialogueGui:Destroy()
    		print("movement unlocked")
    		player.CameraMode = "LockFirstPerson"
    		player.Character.Humanoid.WalkSpeed = 12
    	end
    	if cont.Text == "Continue" and player:FindFirstChild("dialogueGui") then
    		for i = 3, #strings do
    				script.Parent.Parent.ImageLabel.NPCDialogue.Text = string.sub(nextstring,1,i)
    				wait(0.1)
    				end
    	end
    end)

    while true do
    	wait(5)
    	search()
    	print(nextstring)
    	if not next(strings) then
    		script.Parent.Text = "Close"
    	elseif
    		 next(strings) then
    			script.Parent.Text = "Continue"
    	end
    end

So far there are no errors in the output, only the prints from the scripts.
Thank you for reading!

1 Like

Is the ClickDetector script a normal script or a local script? If it’s a normal script, and you’re destroying the UI from within a local script, the UI won’t be gone from the server’s point of view.

1 Like

The UI doesn’t clone (for the second time) because you’re calling :Destroy() on the UI locally. Since it’s local, it will not replicate what you just did to the server, therefore trying to “clone” with that check in place will not work, as the server still sees it as being in the PlayerGui.

You can see this for yourself using Accurate Play Solo’s features (switching from client PoV to server PoV.)

4 Likes