Stuck with a duplicated event. Can't seem to figure out what the problem is. [Knit Framework]

This is the function here. I feel like I am being dumb and this isn’t the most sensible way to do this. Any pointers on where I am going wrong? Don’t want any code snippets, just a brief explanation/post. Print lines were used for debugging to see where it started duplicating. The duplication starts on line self.Client.FromClient:Connect(function(Player, Data).

local function PerformFirstRoll()
		local Hold = false
		Target = UndecidedPlayers[math.random(1, #UndecidedPlayers)]
		for i,v in pairs(Players:GetPlayers()) do
			self.Client.ToClient:Fire(v, {"Roller", Target.Name})
		end
		self.Client.ToClient:Fire(Target, {"PTR"})
		task.wait()
		Hold = true
		self.Client.FromClient:Connect(function(Player, Data)
			print(Player, Data)
			print("S1")
			if Data == "Rolled" then
				print("S2")
				self.Client.ToClient:Fire(Target, {"DisplayRoll"})
				print("S3")
				local Roll = DiceService:Roll()
				print("S4")
				DataService:UpdateAsync({"RollQueue", Player.Name, Roll})
				print("S5")
				Hold = false
				print("S6")
			end
		end)
		while Hold == true do
			task.wait(0.1)
		end
		for i,v in pairs(UndecidedPlayers) do
			if v.Name == Target.Name then
				table.remove(UndecidedPlayers, i)
			end
		end
		print(UndecidedPlayers)
		return true
	end
	
	while #UndecidedPlayers > 0 do
		task.wait(0.5)
		if Active == false then
			Active = true
			local WaitForPerform = PerformFirstRoll()
			if WaitForPerform == true then
				Active = false
			end
		end
	end
``
1 Like

Hi!

I am a little confused on exactly what this does, but I do see one problem.

I assume that you’re saying after one roll, it prints as if the event was ran more than once than it’s supposed to.

This could be because you are not disconnecting the event at all. It connects another function and keeps the old one made from the ‘PerformFirstRoll’ function.

Try replacing that function with this and see what it does:

local function PerformFirstRoll()
	local Hold = false
	local Connection = nil
	Target = UndecidedPlayers[math.random(1, #UndecidedPlayers)]
	for i,v in pairs(Players:GetPlayers()) do
		self.Client.ToClient:Fire(v, {"Roller", Target.Name})
	end
	self.Client.ToClient:Fire(Target, {"PTR"})
	task.wait()
	Hold = true
	Connection = self.Client.FromClient:Connect(function(Player, Data)
		print(Player, Data)
		print("S1")
		if Data == "Rolled" then
			print("S2")
			self.Client.ToClient:Fire(Target, {"DisplayRoll"})
			print("S3")
			local Roll = DiceService:Roll()
			print("S4")
			DataService:UpdateAsync({"RollQueue", Player.Name, Roll})
			print("S5")
			Hold = false
			print("S6")
		end

		if Connection then
			Connection:Disconnect()
			Connection = nil
		end

	end)
	while Hold == true do
		task.wait(0.1)
	end
	for i,v in pairs(UndecidedPlayers) do
		if v.Name == Target.Name then
			table.remove(UndecidedPlayers, i)
		end
	end
	print(UndecidedPlayers)

	return true
end

Make sure to mark this post as solution if it helped you :slight_smile: if not, please reply and let me know so I can further assist you.

I totally forgot about Disconnect. Thank you!

1 Like