How to make an event only fire once in a loop

I would like to know how to make this event fire only once in this loop instead of like, 10 times

script:

	for i, v in pairs(doorsfolders:GetChildren()) do
		if (v.EButtonPart.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).Magnitude <= 30 then
			script.Parent.Parent.Parent.CityGui.Enabled = true
			script.Parent.Parent.Parent.CityGui.E.MouseButton1Click:Connect(function()
				script.Parent.Visible = true
				script.Parent.Title.Value = v.Name
				script.Parent.Price.Value = v.Needed.Value
				script.Parent.Yes.MouseButton1Click:Connect(function()
					if game.Players.LocalPlayer.leaderstats.Orbs.Value >= script.Parent.Price.Value then
						game.Players.LocalPlayer.leaderstats.Orbs.Value -= script.Parent.Price.Value
						v:Destroy()
						script.Parent.Visible = false
						local doorvalue = Instance.new("StringValue") -- event to fire
						doorvalue.Parent = game.Players.LocalPlayer.Doors -- event to fire
						doorvalue.Name = v.Name -- event to fire
					end
				end)
				script.Parent.No.MouseButton1Click:Connect(function()
					script.Parent.Visible = false
				end)
			end)
		else
			script.Parent.Parent.Parent.CityGui.Enabled = false
		end
	end

You could create a debounce/variable such as local attempts = 0

And add 1 to it in your script and in the event part make an if statement to check if the number of attempts is different than 1, if it is, dont fire it, you could return if needed. If it is not, meaning it is 0, then run it.

add a break after checking it’s position.

for i, v in pairs(doorsfolders:GetChildren()) do
		if (v.EButtonPart.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).Magnitude <= 30 then
			script.Parent.Parent.Parent.CityGui.Enabled = true
			script.Parent.Parent.Parent.CityGui.E.MouseButton1Click:Connect(function()
				script.Parent.Visible = true
				script.Parent.Title.Value = v.Name
				script.Parent.Price.Value = v.Needed.Value
				script.Parent.Yes.MouseButton1Click:Connect(function()
					if game.Players.LocalPlayer.leaderstats.Orbs.Value >= script.Parent.Price.Value then
						game.Players.LocalPlayer.leaderstats.Orbs.Value -= script.Parent.Price.Value
						v:Destroy()
						script.Parent.Visible = false
						local doorvalue = Instance.new("StringValue") -- event to fire
						doorvalue.Parent = game.Players.LocalPlayer.Doors -- event to fire
						doorvalue.Name = v.Name -- event to fire
					end
				end)
				script.Parent.No.MouseButton1Click:Connect(function()
					script.Parent.Visible = false
				end)
			end)
			break
		else
			script.Parent.Parent.Parent.CityGui.Enabled = false
		end
	end

thank you very much it works!!

1 Like