Event running many times

  1. Everytime a key is clicked, a remote is fired.

  2. , basically this is a script where it updates a billboardgui adornee on a player and if they press C the event is fired. It runs many times, and causes the notification script not to work.

  3. I’ve tried making a cooldown, does not work.

	wait(0.3)
	for _,v1 in pairs(Interactions:GetChildren()) do
		local v = game.Workspace:FindFirstChild(v1.Name)

		if v.HumanoidRootPart then


			if v then
				if (v.HumanoidRootPart.Position - Player.Character.HumanoidRootPart.Position).magnitude == 0 then  else
				local ray = Ray.new(game.Players.LocalPlayer.Character.HumanoidRootPart.Position, (v.HumanoidRootPart.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position))

				local ignore = {Player.Character}

				local hit,position,normal = game.Workspace:FindPartOnRayWithIgnoreList(ray,ignore)

				if v and (v.HumanoidRootPart.Position - Player.Character.HumanoidRootPart.Position).magnitude < 5  then

					Interaction = hit.Parent
					


	if Interaction then

		
		if Player.PlayerGui:FindFirstChild('InteractiveMenu') then
			if Interaction.IsRestricted.Value >= 0 then

								if game.ReplicatedStorage.CheckPermission:InvokeServer(Interaction.IsRestricted.Value) == false then
					local Int = Player.PlayerGui:FindFirstChild('InteractiveMenu') 
					Int.Adornee = Interaction
					Int.Frame.Visible = true
										Int.Frame.C.Msg.Text = Interaction.Message.Value
										Int.Frame.C.Down.Key.Text = Interaction.Key.Value
										Int.Frame.C.Up.Key.Text = Interaction.Key.Value
										Mouse.KeyDown:Connect(function(Key)
											if db1 == true then return else
											if Int.Frame.C.Visible == true and Int.Adornee and Int.Frame.C.Msg.TextColor3 == Color3.fromRGB(255, 255, 255) then
												if string.upper(Key) == "C" then
													if Interaction.HandcuffValue.Value == false then
													
												local value = Key

													Player.PlayerGui.InteractiveMenu.Click:Play()
													local action = "Cuff"
								spawn(function()
									wait(0.1)
														Int.Frame.C.Up.Visible = false
														Int.Frame.C.Msg.TextColor3 = Color3.fromRGB(89, 255, 0)
														Int.Frame.C.Down.Visible = true
														if db1 == true then end
														db1 = true
																

														
														db1 = false
									wait(0.1)
														Int.Frame.C.Up.Visible = true
														Int.Frame.C.Msg.TextColor3 = Color3.fromRGB(255, 255, 255)
																Int.Frame.C.Down.Visible = false
																game.ReplicatedStorage:FindFirstChild('InteractionEvent'):FireServer(v, Key)
																game.ReplicatedStorage.Notification:FireServer(action, v)
Mouse.KeyDown:Connect(function(Key)

You’re creating this connection inside a of a generic for loop, this means that for each iteration of that for loop the Mouse.KeyDown connection is created. Consequently, if the loop iterates 5 times the connection is made 5 times, and a single keystroke will result in the KeyDown event being fired 5 times, you need to move the connection’s creation to outside of the loop.

This is the answer. Scope is important, and making sure you aren’t in the scope of a loop is even MORE important. :smiley:

How would I close the for loop? I am trying to put end, but it makes the whole script not work

Here’s the full script

local Player = game.Players.LocalPlayer

local Character = Player.Character or Player.CharacterAdded 

local Interactions = game.Workspace.Players

local Mouse = Player:GetMouse()
enabled = false
local NearestInteraction 

local db1 = false
local db2 = false
local db3 = false

local Interaction

while true do
	wait(0.3)
	for _,v1 in pairs(Interactions:GetChildren()) do
		local v = game.Workspace:FindFirstChild(v1.Name)

		if v.HumanoidRootPart then


			if v then
				if (v.HumanoidRootPart.Position - Player.Character.HumanoidRootPart.Position).magnitude == 0 then  else
				local ray = Ray.new(game.Players.LocalPlayer.Character.HumanoidRootPart.Position, (v.HumanoidRootPart.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position))

				local ignore = {Player.Character}

				local hit,position,normal = game.Workspace:FindPartOnRayWithIgnoreList(ray,ignore)

				if v and (v.HumanoidRootPart.Position - Player.Character.HumanoidRootPart.Position).magnitude < 5  then

						Interaction = hit.Parent
					
					
				break
			end
			


	if Interaction then

		
		if Player.PlayerGui:FindFirstChild('InteractiveMenu') then
			if Interaction.IsRestricted.Value >= 0 then

								if game.ReplicatedStorage.CheckPermission:InvokeServer(Interaction.IsRestricted.Value) == false then
					local Int = Player.PlayerGui:FindFirstChild('InteractiveMenu') 
					Int.Adornee = Interaction
					Int.Frame.Visible = true
										Int.Frame.C.Msg.Text = Interaction.Message.Value
										Int.Frame.C.Down.Key.Text = Interaction.Key.Value
										Int.Frame.C.Up.Key.Text = Interaction.Key.Value
		
				local Int = Player.PlayerGui:FindFirstChild('InteractiveMenu') 
				Int.Adornee = Interaction
								Int.Frame.Visible = true
								
				
							
					end
				end
			end

						elseif (v.HumanoidRootPart.Position - Player.Character.HumanoidRootPart.Position).magnitude > 5 then
			local Int = Player.PlayerGui:FindFirstChild('InteractiveMenu') 
			Int.Adornee = nil
			NearestInteraction = nil
			Int.Frame.Visible = false
							
						end
					
					
			
					end end
					end
					end
				end
			
			
	

Alright, I was able to break the loop, but now one person can press the button and it fires, and the other person isn’t able to do it.