Function is called as many times as the player has completed a number of quests

Hello guys !
Im here today because I am having a problem with my quest system, when the player complete a quest it works perfectly but the 2nd quest he complete the function is called 2 times and the reward are given 2 times so. When I complete 3 times a quest that give me 3 times the rewards

questProgress:GetPropertyChangedSignal("Value"):Connect(function()
		if questTask.Value ~= "" then
			if questProgress.Value >= questGoal.Value then
				plr.PlayerGui:WaitForChild("Quests").Canvas.Main.Details.ClaimBTN.Visible = true
				plr.PlayerGui:WaitForChild("Quests").Canvas.Main.Details.ClaimBTN.ClaimBTN.MouseButton1Click:Connect(function()
					local questnum = plr:WaitForChild("Quest"):FindFirstChild(questGiver.Value):FindFirstChild(questNumber.Value)
					questnum.Value = true
					if plr:WaitForChild("BoostCount").Value == plr:WaitForChild("MaxBoost").Value then 
						local template = plr.PlayerGui.PopUpGUI.MainFrame.Template:Clone()
						template.Parent = plr.PlayerGui.PopUpGUI.MainFrame
						template.Text = "You can't store more boosts !"
						template.Visible = true
						game.ReplicatedStorage.Remotes.PlaySound:FireClient(plr, "WrongSFX")
						wait(1.5)
						template:Destroy()
					else
						plr:WaitForChild(questRewardBoostFolder.Value):WaitForChild(questRewardBoost.Value).Value += questReward.Value
						plr.PlayerGui:WaitForChild("Quests").Canvas.Main.Details.ClaimBTN.Visible = false
						remotes.StoreBoost:FireClient(plr, questRewardBoost.Value)
						for i, v in pairs(quest:GetChildren()) do
							if v:IsA("StringValue") then
								v.Value = "You finished the quest !"
							elseif v:IsA("NumberValue") or v:IsA("IntValue") then
								v.Value = 0
							end
						end
						wait(3)
						for i, v in pairs(quest:GetChildren()) do
							if v:IsA("StringValue") then
								v.Value = ""
							elseif v:IsA("NumberValue") or v:IsA("IntValue") then
								v.Value = 0
							end
						end
					end
				end)
			end	
		end
	end)

idk if i am clear say it if im not clear ill try to reexplain it
Here is my code if you guys can help me i’ll really appreciate it :slight_smile:

Every time a quest is completed, you are connecting a new event listener to the MouseButton1Click event of the ClaimBTN. This results in multiple listeners being added, which causes the function to be called multiple times.

You need to make sure that the event listener is connected only once. You can achieve this by disconnecting any existing listeners before connecting a new one, or by connecting the listener only once outside the GetPropertyChangedSignal function.

local function handleClaimButtonClick()
	local questnum = plr:WaitForChild("Quest"):FindFirstChild(questGiver.Value):FindFirstChild(questNumber.Value)
	questnum.Value = true
	if plr:WaitForChild("BoostCount").Value == plr:WaitForChild("MaxBoost").Value then 
		local template = plr.PlayerGui.PopUpGUI.MainFrame.Template:Clone()
		template.Parent = plr.PlayerGui.PopUpGUI.MainFrame
		template.Text = "You can't store more boosts !"
		template.Visible = true
		game.ReplicatedStorage.Remotes.PlaySound:FireClient(plr, "WrongSFX")
		wait(1.5)
		template:Destroy()
	else
		plr:WaitForChild(questRewardBoostFolder.Value):WaitForChild(questRewardBoost.Value).Value += questReward.Value
		plr.PlayerGui:WaitForChild("Quests").Canvas.Main.Details.ClaimBTN.Visible = false
		remotes.StoreBoost:FireClient(plr, questRewardBoost.Value)
		for i, v in pairs(quest:GetChildren()) do
			if v:IsA("StringValue") then
				v.Value = "You finished the quest !"
			elseif v:IsA("NumberValue") or v:IsA("IntValue") then
				v.Value = 0
			end
		end
		wait(3)
		for i, v in pairs(quest:GetChildren()) do
			if v:IsA("StringValue") then
				v.Value = ""
			elseif v:IsA("NumberValue") or v:IsA("IntValue") then
				v.Value = 0
			end
		end
	end
end

-- Connect the event listener only once
plr.PlayerGui:WaitForChild("Quests").Canvas.Main.Details.ClaimBTN.ClaimBTN.MouseButton1Click:Connect(handleClaimButtonClick)

questProgress:GetPropertyChangedSignal("Value"):Connect(function()
	if questTask.Value ~= "" then
		if questProgress.Value >= questGoal.Value then
			plr.PlayerGui:WaitForChild("Quests").Canvas.Main.Details.ClaimBTN.Visible = true
		end	
	end
end)
1 Like

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