Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does the Gui get cloned more than once?

Asked by
Gunzyn 2
6 years ago

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

Log in to vote
0
Answered by
lukeb50 631 Moderation Voter
6 years ago

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)
0
Well, that works, but once you say "X" you can keep using "Help" even though "XReaction" Gui gets destroyed. Gunzyn 2 — 6y
0
Then just set X to nil after it is destroyed lukeb50 631 — 6y
Ad

Answer this question