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

How i can break this loop for clone the weapon only 1 time?

function Equip(weapon)
	if debounce == true then
		debounce = false
		if Character then
			if WeaponRequest:InvokeServer(weapon) == 1 then
				for _, v in pairs(Character:GetChildren()) do
					if v:IsA("Tool") then
						if v.Name ~= weapon then
							v:Destroy()
							for t, u in pairs(player.Backpack:GetChildren()) do
								if u.Name == weapon then
									print("d")
									local yy = u:Clone()
									yy.Parent = Character
								end
							end
							refreshUI(weapon)
						else
							v:Destroy()
							refreshUI("niente")
						end
					else
						for t, u in pairs(player.Backpack:GetChildren()) do
							if u.Name == weapon then
								local yy = u:Clone()
								yy.Parent = Character
							end
						end
						refreshUI(weapon)
					end
				end
			end
			debounce = true
		end
	end
end

If i try to run this function, i clone the weapon 33 times inside the Character, and not only 1. How i can fix?

1 comment

0

Where do you want to stop the loop?

1 answer

I placed it on line 30

local hasStoppedOnce = false

function Equip(weapon)
    if debounce == true then
        debounce = false
        if Character then
            if WeaponRequest:InvokeServer(weapon) == 1 then
                for _, v in pairs(Character:GetChildren()) do
                    if v:IsA("Tool") then
                        if v.Name ~= weapon then
                            v:Destroy()
                            for t, u in pairs(player.Backpack:GetChildren()) do
                                if u.Name == weapon then
                                    print("d")
                                    local yy = u:Clone()
                                    yy.Parent = Character
                                end
                            end
                            refreshUI(weapon)
                        else
                            v:Destroy()
                            refreshUI("niente")
                        end
                    else
                        for t, u in pairs(player.Backpack:GetChildren()) do
                            if u.Name == weapon then
                                local yy = u:Clone()
                                yy.Parent = Character
                            end
			    if hasStoppedOnce = false then
				break
				hasStoppedOnce = true
			    end
                        end
                        refreshUI(weapon)
                    end
                end
            end
            debounce = true
        end
    end
end

2 comments

0

Not work

0

oh you dont have to accept then. Then idk the answer

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