RemoteEvent Firing Multiple times for no apparent reason

I am creating a story mode type game. And It uses a dialogue system. Whenever the player talks to that specific dialogue their story mode Position gets updated to the data store so that when they load back in they are still at the point they were when they left. In order for them to save that point they need to talk to a specific npc that only shows up for them if they have reached a story mode point. I already set up a dialogue system for them to talk with the npc but I am running into a bug with it.

Whenever I run this code: (Server)

local function Dialouge(Plr, TextLabel, Text, YesNo, Ok, BorderColorGradient, BackgroundColorGradient, TextColorGradient, Rate, DialougePrompt, StoryMode)
print ("Recived DialougeFunction")
local MainBackground = TextLabel.Parent.Parent.MainBackground
MainBackground.UIGradient.Color = BackgroundColorGradient

local MainBorder = TextLabel.Parent.Parent.MainBorder
MainBorder.UIGradient.Color = BorderColorGradient

local Texts = {}
table.insert(Texts, TextLabel)
table.insert(Texts, TextLabel.Parent.Parent.YesButton)
table.insert(Texts, TextLabel.Parent.Parent.ByeButton)
table.insert(Texts, TextLabel.Parent.Parent.OkButton)

for i,String in pairs (Texts) do
	String.UIGradient.Color = TextColorGradient
end
print ("DoingYesNoCheck")
if YesNo == true then
	print ("PassedYesNo")
	InitiateYesNoDialougeEvent:FireClient(Plr, DialougePrompt, TextLabel)
	print ("FiredInitiateYesNoEvent")
	wait(3)
	print ("TypingOut Text")
	TypeOut(TextLabel, Text, Rate)
	wait(string.len(Text)*Rate)
	for i,TextObject in pairs (Texts) do
		if TextObject.Name == "YesButton" or "ByeButton" then
			TextObject.Active = true
		end
	end
	print ("Enabled YesNoButtons")
	ChooseYesNoClientEvent:FireClient(Plr) --Here
	print ("FiredChooseYesNo Event")
	
	ChooseYesNoServerEvent.OnServerEvent:Connect(function(PlrWhoFired, YesNo)
		print ("RecivedYesNoFromPlayer")
		for i,TextObject in pairs (Texts) do
			if TextObject.Name == "YesButton" or "ByeButton" then
				TextObject.Active = false
			end
		end
		if YesNo == "Yes" then
			print ("PlayerPressedYes")
			if StoryMode ~= nil then
				PlrWhoFired.AttributeFolder.StoryPoint.Value = StoryMode
			end
			EndDialougeEvent:FireClient(Plr, Texts, DialougePrompt)
			wait (1.1)
			TextLabel.Text = ""
		elseif YesNo == "No" then
			print ("PlayerPressedNo")
			EndDialougeEvent:FireClient(Plr, Texts, DialougePrompt)
			wait (1.1)
			TextLabel.Text = ""
		else
			warn("Error")
		end
	end)

What this code does is that It gets the attributes from the specific proximity prompt and creates the dialogue with it. Where I put “Here” is where it fires to the client this code: (Client)

local function ChooseYesNo()
print ("RecivedChooseYesNo")
local tweenProperties = {
	TextStrokeTransparency = 0,
	TextTransparency = 0
}

local Tween1 = TweenService:Create(YesButton, tweenInfo, tweenProperties)
local Tween2 = TweenService:Create(ByeButton, tweenInfo, tweenProperties)
Tween1:Play()
Tween2:Play()
print ("PlayingTweens")
wait(0.7)
Tween1:Pause()
Tween2:Pause()

YesButton.MouseButton1Click:Connect(function()
	print ("PlayerPressedYes")
	local YesNo = "Yes"
	ChooseYesNoServerEvent:FireServer(YesNo)
	ByeButton.Active = false
	Tween1:Play()
	Tween2:Play()
end)

ByeButton.MouseButton1Click:Connect(function()
	print ("PlayerPressedNo")
	local YesNo = "No"
	ChooseYesNoServerEvent:FireServer(YesNo)
	ByeButton.Active = false
	Tween1:Play()
	Tween2:Play()
end)

end

What this does is makes it so that the player can pick either a yes or no button. Once they choose yes or no this code runs:

		ChooseYesNoServerEvent.OnServerEvent:Connect(function(PlrWhoFired, YesNo)
		print ("RecivedYesNoFromPlayer")
		for i,TextObject in pairs (Texts) do
			if TextObject.Name == "YesButton" or "ByeButton" then
				TextObject.Active = false
			end
		end
		if YesNo == "Yes" then
			print ("PlayerPressedYes")
			if StoryMode ~= nil then
				PlrWhoFired.AttributeFolder.StoryPoint.Value = StoryMode
			end
			EndDialougeEvent:FireClient(Plr, Texts, DialougePrompt)
			wait (1.1)
			TextLabel.Text = ""
		elseif YesNo == "No" then
			print ("PlayerPressedNo")
			EndDialougeEvent:FireClient(Plr, Texts, DialougePrompt)
			wait (1.1)
			TextLabel.Text = ""
		else
			warn("Error")
		end
	end)

So what this code does is that It receives the “YesNo” Variable that the player sent. If the variable is the string “Yes” then it sets the storypoint attribute, if it is no then it ends the dialogue.

The error that I’m getting is that when I click on the client the Yes button it spams the remote multiple times, and it does not set the story point attribute.

Can someone please help me?

I haven’t read through your code yet, but add debounces?

local debounce = false

if not debounce then
   debounce = true
   --Code here
   --If you want, set debounce to false, so it can fire again. 
end

You can add something like this when your remote event fires, at the start of the code, etc. Have you done that?

Didn’t give too much to work with about what you are actually attempting but if you only want the RemoteEvent to fire once when they click a certain button I would use MouseButton1Down instead of MouseButton1Click because it checks less. I think it would work in your situation.

What is calling this function? And why did you put the MouseButton connections within them?

If you call this function multiple times you will effectively be connecting multiple MouseButton connections. Which will result in 1 click = multiple connections being fired. I suggest leaving your connections outside the function or disconnecting them once a selection has been made!

Note the above only applies if you call ChooseYesNo() more than once!

1 Like

Thank you! I have only been scripting for 4 months and I didn’t know :Disconnect() was a thing. Thank you.