Hi, so i recently started learning OOP and this is my first script ever using OOP. So basically what this does is it makes a quest ,where the player has to kill 6 npc’s, whenever the method is fired and I was wondering if i could improve anything about the script in general and maybe improve efficiency
local Quests = {}
Quests.__index = Quests
function Quests.QuestData(player)
local LocalData = {}
LocalData.Player = player
LocalData.Key = player.UserId.."-Quests"
LocalData.Savingquests = {}
for i,v in pairs(player.ActiveQuests:GetChildren()) do
table.insert(LocalData.Savingquests,v.Name)
table.insert(LocalData.Savingquests,v.Value)
end
LocalData.DataStore = game:GetService("DataStoreService"):GetDataStore("Quests")
setmetatable(LocalData, Quests)
return LocalData
end
function Quests:save()
local savedData
local succes,errormsg = pcall(function()
savedData = self.DataStore:SetAsync(self.Key,self.Savingquests)
end)
if succes then
print("saved")
else
warn(errormsg)
end
end
function Quests:load()
print("loadingdata")
local LoadedData
local succes, errormsg = pcall(function()
LoadedData = self.DataStore:GetAsync(self.Key)
end)
if LoadedData then
print("datafound")
for i,data in pairs(LoadedData) do
print(data)
if data == "KillNPC" then
print("making quest")
local sendingdata = setmetatable({plr= self.Player, amount = LoadedData[i+1],kills = 6},Quests)
sendingdata:KillNPCQuest()
end
end
end
end
function Quests:MakenewQuest(questinfo)
local info = questinfo
info.Progress = 0
setmetatable(info,self)
if info.questType == "KillNPC" then
info:KillNPCQuest()
end
return info
end
function Quests:KillNPCQuest()
local Quest = Instance.new("IntValue")
Quest.Value = self.amount or 0
Quest.Name = "KillNPC"
Quest.Parent = self.aq or self.plr.ActiveQuests
Quest.Changed:Connect(function()
if Quest.Value == self.kills then
local ExpToGive = 100
self.plr.Stats.Exp.Value += ExpToGive
Quest:Destroy()
end
end)
end
return Quests
Normal script:
-- Servieces
local DSS = game:GetService("DataStoreService")
local QuestsDataStore = DSS:GetDataStore("QuestsDataStore")
local RS = game:GetService("RunService")
local QuestModule = require(script.Quests)
game.Players.PlayerAdded:Connect(function(player)
local ActiveQuests = Instance.new("Folder")
ActiveQuests.Name = "ActiveQuests"
ActiveQuests.Parent = player
QuestModule:MakenewQuest({kills = 6, plr = player,aq = ActiveQuests,questType = "KillNPC"})-- this will make the quest but u need to require the module script
local playerData = QuestModule.QuestData(player)
playerData:load()
end)
local function savedata(player)
local playerData = QuestModule.QuestData(player)
if playerData then
playerData:save()
end
end
game.Players.PlayerRemoving:Connect(function(player)
savedata(player)
end)
game:BindToClose(function()
if not RS:IsStudio() then
for i,v in pairs(game.Players:GetPlayers()) do
savedata(v)
end
while true do
wait()
end
else
wait(5)
end
end)