Quest Module Code Review

Hi everyone, I made a quest module. Im waiting for your feedback.
You can get from here.

Module Link : https://create.roblox.com/store/asset/84514548913491/MythQuest?assetType=Model&externalSource=www

Baseplate Link : Quest Module Test Place - Roblox

Server Side Code Preview

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--[[
	MODULES
	==============
]]

local Library = ReplicatedStorage.Library
local MythQuest = require(Library.MythQuest)

--[[
	CODE
	===============
]]

local function InitializeQuestParts()
	for _, part: BasePart in workspace.QuestParts:GetChildren() do
		
		-- Sec for base part
		if ( not part:IsA("BasePart") ) then
			continue
		end
		
		local touchDebounces = {}
		
		part.Touched:Connect(function(hit)
			local humanoid = hit.Parent and hit.Parent:FindFirstChild('Humanoid')
			if ( not humanoid ) then return end
			
			local player = Players:GetPlayerFromCharacter(hit.Parent)
			if ( not player ) then return end
			
			if ( touchDebounces[player] ) then return end
			touchDebounces[player] = true
			
			task.delay(1, function()
				touchDebounces[player] = nil
			end)
			
			local manager = MythQuest.GetUser(player)
			if ( not manager ) then
				warn('The player has not quest manager')
				return
			end
			
			local quest = manager.GetQuestByName(part.Name)
			if ( quest ) then
				
				if ( not quest:IsStarted() ) then
					quest:Start()
				end
				
				return quest:Progress(1)
			end
			
			quest = manager.CreateQuest({
				Name = part.Name,
				Tag = "",
				Progress = 0,
				Goal = 10
			})
			
			quest:Start()
			
		end)
	end
end

local function PlayerAdded(player: Player)
	local manager = MythQuest.new(player)
	
	manager.QuestCreated:Connect(function(quest)
		print("Quest Created: ", quest)
	end)
	
	manager.QuestStarted:Connect(function(quest)
		print("Quest Started: ", quest)
	end)
	
	manager.QuestStateChanged:Connect(function(oldState, newState)
		print("Quest State Changed: ", oldState, newState)
	end)
	
	manager.QuestCanceled:Connect(function(quest)
		print('Quest Canceled: ', quest)
	end)
	
	manager.QuestCompleted:Connect(function(quest)
		print('Quest Completed: ', quest)
	end)
end

local function PlayerRemoving(player: Player)
	MythQuest.CleanUp(player)
end

for _, player: Player in Players:GetPlayers() do
	task.spawn(PlayerAdded, player)
end

Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)

InitializeQuestParts()

Client Side Code Preview

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Library = ReplicatedStorage:WaitForChild('Library')
local Manager = require(Library:WaitForChild('MythQuest'))

Manager.QuestCreated:Connect(function(quest)
	print(quest)
end)

Manager.QuestStarted:Connect(function(quest)
	print(quest)
end)

Manager.QuestCompleted:Connect(function(quest)
	print(quest)
end)

Manager.QuestCanceled:Connect(function(quest)
	print(quest)
end)

Manager.QuestStateChanged:Connect(function(oldState, newState)
	print(oldState, newState)
end)

Manager.RequestData() -- Fires events if client not loaded before ( lost data )
2 Likes

Looks good! I wish you success!