How to make a quest system?

Alright so I’m currently trying to make a quest system and this actually seems to be pretty difficult, but I’m gonna try my best to make one. So I have the UI part of it done so far I just lop through my module script and then display the quests. This is my module script.

local module = {
	
	["Test Quest 1"] = {
		["Objective"] = "Kill 10 enemies",
		["Description"] = "",
		["Rewards"] = {

		},
	},
	["Test Quest 2"] = {
		["Objective"] = "Get 100 gold",
		["Description"] = "",
		["Rewards"] = {

		},

	},
	["Test Quest 3"] = {
		["Objective"] = "Get to level 5",
		["Description"] = "",
		["Rewards"] = {

		},
	},
	
}

return module

Now the issue I have is how do I actually make the quest? How would I track if the player kills 10 enemies or earns 100 gold etc I can’t seem to figure out that.

I know I’ll have to use data stores and then probably store the quest and the progress in it but I’m not sure how if listen for changes and stuff like that

2 Likes

I’d take a page out of the Elder Scrolls games and use “states” that relate to requirements and counters.

Let’s say you have Quest #1 and it has 3 states so you can store the save data like so:

  1. Started the quest
  2. Killed 10 monsters
  3. Quest is done, turned it in and got reward
1 = {
	state = 1;
	customData = {
		kills = 0;
	}
}

Now, let’s say every quest has its own module script. Like this

-- script name "quest.quest1"
local quest1 = {}

quest1.killsNeeded = 10;

function quest1:getSaveData()
	return {
		state = 1;
		customData = {
			kills = 0;
		}
	}
end

function quest1:onCompleted(player, currentState)
-- Move the state + 1 and run some custom code for the currentState + 1
end

This way, every quest is organized and manages its own save data. So when the quest is started, you can simply add this to the player’s “Quests” save key

local savedQuestData = {}; -- Load their data
savedQuestData[1] = quest1:getSaveData(); -- To start the quest, and save it

Now, your game should be organized to where you track kills of anything globally in a single place (hopefully) and you can simply do this upon killing of a mob

-- upon death of a mob
if (mobID == "someMonsterName") then
	-- Check they're actually on quest 1 and are still doing it (state 1)
	if (playerQuests.quest1 and playerQuests.quest1.state == 1) then
		playerQuests.quest1.customData.kills += 1
		if (playerQuests.quest1.customData.kills >= quest1Module.killsNeeded) then
			quest1Module:onCompleted(player, 1)
		end
	end
end

Which is how TES does it, in a nutshell.

9 Likes

If you need other help, there are multiple tutorials on this if you want.

I don’t have away to track kills although I do have a module script where I create enemies and have a thing for when they die so I suppose I can use that? However what I wanna ask is why would I make a module script for each quest? If I had like 100 quests I don’t feel like making 100 module scripts is the right way lmao

That’s the nature of object-oriented programming. I don’t see why having 100 module scripts is a bad thing if it keeps all of your code uniquely separated and manageable. It’s very common to do this on big projects.

But if you’d prefer to have like 1 excessively long script, we can’t really stop you I guess.