Basically, I created a simple module to handle quests and wanted to do some tests. At first, it was working perfectly.
However, I noticed that when I cancel a quest and try to do it again, it kind of recreates the old folder and the old StringValue again, and creates a new folder and a new StringValue, becoming “duplicated”.
I tried several ways to solve it but I couldn’t and I didn’t find out the reason for this bug to happen. Could any kind soul help me solve this problem that made me lose billions of neurons? I want to thank you in advance.
Module:
local QuestFramework = {}
local Remotes = game:GetService("ReplicatedStorage").Remotes
local RunService = game:GetService("RunService")
local Debounce = false
local function SetText(Player, Text)
local TextLabel = Player.PlayerGui.QuestUI.NPCTalking.TextNPC
for i = 1, #Text do
TextLabel.Text = string.sub(Text, 1, i)
task.wait(0.04)
end
end
local function CreateKillsCounter(Player)
local CounterFolder = Instance.new("Folder", Player)
CounterFolder.Name = "CounterFolder"
local KillsCounter = Instance.new("IntValue", CounterFolder)
KillsCounter.Name = "KillsCounter"
KillsCounter.Value = 0
end
local function DeleteKillsCounter(Player)
Player.CounterFolder:Destroy()
end
local function DefineEnemy(Player, EnemyType)
local LookingFor = Instance.new("StringValue", Player)
LookingFor.Name = "LookingFor"
LookingFor.Value = EnemyType
end
local function StopLookingFor(Player)
Player.LookingFor:Destroy()
end
QuestFramework.Mission = function(EnemyType, MissionType, XPQuantity, Player)
if MissionType == "Kill" then
Player.Character:AddTag("TalkingToNpc")
local EnemyToKill = EnemyType
local QuestUI = Player.PlayerGui.QuestUI
local MissionUI = Player.PlayerGui.MissionUI
QuestUI.Enabled = true
SetText(Player, "Hello stranger! Are you interested in doing me a small favor in exchange for some reward? You kill 5 " .. tostring(EnemyToKill) .. "'s and you'll get some XP!")
QuestUI.NPCTalking.Accept.Visible = true
QuestUI.NPCTalking.Refuse.Visible = true
Remotes.AnswerQuest.OnServerEvent:Connect(function(Player, Answer)
if Answer == "Accept" then
Player.Character:RemoveTag("TalkingToNpc")
Player.Character:AddTag("DoingQuest")
QuestUI.NPCTalking.Accept.Visible = false
QuestUI.NPCTalking.Refuse.Visible = false
QuestUI.Enabled = false
MissionUI.Enabled = true
MissionUI.Mission.Objective.Text = "OBJECTIVE: Kill 5 " .. tostring(EnemyType) .. "'s!"
CreateKillsCounter(Player)
DefineEnemy(Player, EnemyType)
Remotes.CancelQuest.OnServerEvent:Connect(function()
MissionUI.Enabled = false
Player.Character:RemoveTag("DoingQuest")
StopLookingFor(Player)
DeleteKillsCounter(Player)
end)
RunService.Heartbeat:Connect(function()
if Debounce then return end
Debounce = true
MissionUI.Mission.Kills.Text = tostring(Player.CounterFolder.KillsCounter.Value) .. "/5"
task.wait(1.5)
if Player.CounterFolder.KillsCounter.Value == 5 then
Player.Character:RemoveTag("DoingQuest")
StopLookingFor(Player)
MissionUI.Enabled = false
DeleteKillsCounter(Player)
Player.UserData.XP.Value = Player.UserData.XP.Value + XPQuantity
return
end
Debounce = false
end)
elseif Answer == "Refuse" then
Player.Character:RemoveTag("TalkingToNpc")
QuestUI.NPCTalking.Accept.Visible = false
QuestUI.NPCTalking.Refuse.Visible = false
QuestUI.Enabled = false
end
end)
end
end
return QuestFramework
Here’s the problem (folder and stringvalue being duplicated).