For i,v in pairs printing twice. Why?

This is part of a “Compulsion” script where you can make another player do something but it prints/fires the event twice so it doubles the effect. Can anyone tell me why?

local Events = game.ReplicatedStorage.CharacterFiles.VampireEvents
local Buttons = script.Parent.Buttons
local Victim = script.Parent:WaitForChild("Victim")
local player = game.Players.LocalPlayer

for i,v1 in pairs(Buttons:GetChildren()) do
	v1.MouseButton1Down:Connect(function()
	for i,v2 in pairs(Events:GetChildren()) do
		for i,victim in pairs(game.Workspace:GetChildren()) do
				if v1.Name == v2.Name and Victim.Value == victim.Name  then
				print(v1)
				v2:FireServer(player,victim)
				end
			end
		end
	end)
end

The reason why it prints/fires the event twice is because you have two for loops nested in each other. The inner for loop is looping through all the characters in the game workspace and firing the event if the button name matches the event name and the victim value matches the character name. Since you have the same event firing for each character, that event is being fired multiple times and thus being printed multiple times. To fix this, you can remove the inner for loop and just have the event fire for the specified victim.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.