Creating a quest system in Roblox can add a lot of depth to your game by giving players objectives to complete for rewards. Below is a step-by-step guide to creating a basic quest system using scripts. This guide will help you build a simple quest where players can interact with NPCs, collect items, and complete the quest for a reward.
Quest System Overview
• Players will interact with NPCs to receive quests.
• The NPC will give the player an objective, such as collecting items.
• Once the player completes the objective, they return to the NPC to complete the quest.
• The quest completion will trigger a reward for the player, like in-game currency or a special item.
Step-by-Step Guide to Creating a Quest System
1. Set Up the Quest NPC
1. Create the NPC Model:
• First, create an NPC model. This can be a simple humanoid model, and you should add a Humanoid object to it for it to act like a player character.
• In the Explorer window, right-click and create a new Model. Name it QuestNPC.
• Inside the NPC, add a Head, Torso, Humanoid, and HumanoidRootPart (use basic parts or pre-made models from the Roblox Toolbox).
2. Add Quest Data to the NPC:
• Right-click on the QuestNPC model and create a Folder inside it called Quest.
• Inside the Quest folder, create the following objects:
• StringValue: Name it QuestName (to store the quest name, e.g., “Collect 5 Apples”).
• IntValue: Name it QuestProgress (this will track how many objectives have been completed).
• IntValue: Name it QuestGoal (this will hold the number of items to collect).
• BoolValue: Name it QuestCompleted (this will track whether the quest has been completed).
2. Script to Handle the Quest System
Create a script inside the QuestNPC model to manage the quest logic. This script will handle interactions, quest progress, and rewarding the player.
1. Add the Quest Script:
• Right-click on the QuestNPC model and insert a Script.
2. Copy and Paste the Script Below:
local npc = script.Parent
local questFolder = npc:WaitForChild(“Quest”)
local questName = questFolder:WaitForChild(“QuestName”)
local questProgress = questFolder:WaitForChild(“QuestProgress”)
local questGoal = questFolder:WaitForChild(“QuestGoal”)
local questCompleted = questFolder:WaitForChild(“QuestCompleted”)
local interactDistance = 10 — Distance at which player can interact with NPC
— Function to give the quest to the player
local function giveQuest(player)
— Check if the player already has the quest
if questCompleted.Value then
print(player.Name .. ” has already completed the quest!”)
return
end
— Display quest details (you can expand this to show in a GUI)
print(player.Name .. ” has started the quest: ” .. questName.Value)
print(“Objective: Collect ” .. questGoal.Value .. ” items.”)
— Set the quest progress to 0 when starting
questProgress.Value = 0
end
— Function to update quest progress
local function updateQuestProgress(player)
if questCompleted.Value then
print(player.Name .. ” has already completed the quest!”)
return
end
— Increment quest progress (this can be triggered when player collects items)
questProgress.Value = questProgress.Value + 1
print(player.Name .. ” has collected an item! Progress: ” .. questProgress.Value .. “/” .. questGoal.Value)
— Check if the player has completed the quest
if questProgress.Value >= questGoal.Value then
completeQuest(player)
end
end
— Function to complete the quest
local function completeQuest(player)
questCompleted.Value = true
print(player.Name .. ” has completed the quest: ” .. questName.Value)
— Reward the player (example: give an in-game currency or item)
— For now, we just print a message, but you can add a reward system here.
print(player.Name .. ” receives a reward for completing the quest!”)
— You can add code here to give the player an item or currency as a reward.
end
— Detect when a player interacts with the NPC
npc.Touched:Connect(function(hit)
local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
if player then
— Start the quest if the player is close enough
if (npc.HumanoidRootPart.Position – player.Character.HumanoidRootPart.Position).Magnitude < interactDistance then
giveQuest(player)
end
end
end)
— Function to simulate collecting items (this should be triggered when the player picks up items)
game.ReplicatedStorage.ItemCollected.OnServerEvent:Connect(function(player)
updateQuestProgress(player)
end)
Explanation of the Script:
• Quest Details: The NPC has quest-related data such as the name of the quest (QuestName), how many items the player needs to collect (QuestGoal), and the player’s progress (QuestProgress).
• giveQuest function: This function is triggered when the player interacts with the NPC (i.e., comes within a certain range). It gives the player the quest and sets the progress to 0.
• updateQuestProgress function: This function increments the quest progress. It should be triggered when the player collects an item or completes an action required for the quest.
• completeQuest function: When the player meets the objective (e.g., collecting the required number of items), this function completes the quest and rewards the player. You can customize this part to give an item or in-game currency.
• Proximity Detection: The NPC listens for players getting close to it (via Touched) and triggers the quest when they are within 10 studs (adjustable).
3. Simulate Item Collection for the Quest
To simulate collecting items (such as picking up an apple), you need to set up an event that updates the player’s quest progress.
1. Create the Item (e.g., Apple):
• Insert a Part in the game (like an apple model) and name it Apple.
• Make sure the apple part has CanCollide set to false and Anchored set to false, so players can walk through it.
2. Create a RemoteEvent to Track Item Collection:
• In ReplicatedStorage, right-click and insert a RemoteEvent. Name it ItemCollected.
3. Script to Handle Item Collection:
• Insert a Script into the apple part to detect when a player touches it and triggers the item collection event.
local apple = script.Parent
local remote = game.ReplicatedStorage:WaitForChild(“ItemCollected”)
apple.Touched:Connect(function(hit)
local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
if player then
remote:FireServer(player) — Fire the event to update quest progress
apple:Destroy() — Remove the apple once collected (optional)
end
end)
4. Testing the Quest System:
• When the player touches the QuestNPC, they will receive the quest and be notified of their objective.
• As the player collects Apple items (or any other