Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does the Gui get cloned more than once?

Hello! I made a script that after player says "X", it gives him the first Gui, which shows him options to say, which would give him more Guis. Saying "Help" is supposed to destroy the first Gui, and give him another. The problem is, each time you say "x" "help" to open the second Gui, it gets cloned one more time than last time. For example, first time I say it, Gui gets cloned once, which is good. Then the second time it gets cloned twice, then 3 times and so on.

Is it because I did something wrong? How would I stop this?

This is the script I have right now:

game.Players.PlayerAdded:connect(function(player)
	player.Chatted:connect(function(msg)
		msg = string.lower(msg)
		if msg == "x" then --This works fine, it gives player a gui
			local Player = game.Players.LocalPlayer
			local Found = Player.PlayerGui:FindFirstChild("ReactionToX")
			local XReaction = game.ServerStorage.ReactionToX
			local XReactionClone = XReaction:Clone()
			print("X is being called by " .. player.Name)
			wait(0.7)
			if Found == nil then
				XReactionClone.Parent = Player.PlayerGui
			end
			
			player.Chatted:connect(function(msg)
				msg = string.lower(msg)
				if msg == "help" then --After Player says "x", he can say help to get another gui, and destroy the first one.
					local Help = game.ServerStorage.Help
					local HelpClone = Help:Clone()
					wait(0.5)
					XReactionClone:Destroy()
					wait(0.5)
					HelpClone.Parent = Player.PlayerGui --Here's the problem. It's good if I only try to get this once, but if I say "x" and then "help" two times, the second time it's going to get cloned twice. So, I would have 3 clones.
				end
			end)
		end
	end)
end)

As you can see, both Guis are in ServerStorage.

I hope what I said is understandable. Thank you in advance.

1 answer

you have a chatted function inside of the other that is causing this problem. Here is the code that would avoid this problem:

game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(msg)
        msg = string.lower(msg)
        if msg == "x" then --This works fine, it gives player a gui
            Player = game.Players.LocalPlayer
            local Found = Player.PlayerGui:FindFirstChild("ReactionToX")
            local XReaction = game.ServerStorage.ReactionToX
            XReactionClone = XReaction:Clone()
            print("X is being called by " .. player.Name)
            wait(0.7)
            if Found == nil then
                XReactionClone.Parent = Player.PlayerGui
            end
		elseif msg == "help" and XReactionClone~=nil then --After Player says "x", he can say help to get another gui, and destroy the first one.
           local Help = game.ServerStorage.Help
           local HelpClone = Help:Clone()
           wait(0.5)
           XReactionClone:Destroy()
           wait(0.5)
           HelpClone.Parent = Player.PlayerGui --Here's the problem. It's good if I only try to get this once, but if I say "x" and then "help" two times, the second time it's going to get cloned twice. So, I would have 3 clones.
		end
    end)
end)

2 comments

0

Well, that works, but once you say "X" you can keep using "Help" even though "XReaction" Gui gets destroyed.

0

Then just set X to nil after it is destroyed

This discussion is preserved as it was. New answers, comments, and votes are closed.