Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there anyway to make a better quest system than this?

Asked by
LuaDLL 253 Moderation Voter
3 years ago

I made a little quest system for my simulator game I'm making but I don't know if there is a way to make it better.

Script:

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

local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local DataChanged = Remotes:WaitForChild("DataChanged")
local Datastore2 = require(ReplicatedStorage:FindFirstChild("Datastore2"))
local replicatedData = ReplicatedStorage:FindFirstChild("replicatedData")
local Quests = require(ReplicatedStorage:WaitForChild("Quests"))

local DataNum = ReplicatedStorage:WaitForChild("DataNum")

function checkQuests(player, typ, data) -- Checks Quests
    local QuestData = Datastore2("Quest"..DataNum.Value, player)
    local InventoryData = Datastore2("Inventory"..DataNum.Value, player)
    local PetData = Datastore2("Pets"..DataNum.Value, player)
    local CoinData = Datastore2("Coins"..DataNum.Value, player)
    local DiamondData = Datastore2("Diamonds"..DataNum.Value,player)
    local playerQuestData = QuestData:Get({})
    for QuestName,QuestTable in pairs(Quests) do
        if typ == "Inventory" then
            if (data[QuestTable.Food] and not playerQuestData[QuestName]) then
                if (data[QuestTable.Food] >= QuestTable.Amount) then
                    local Reward = QuestTable.Reward
                    local Type = Reward.Type
                    local Value = Reward.Value
                    local Amount = Reward.Amount
                    QuestData:Update(function(currentData)
                        currentData[QuestName] = true
                        return currentData
                    end)
                    if Type == "Coins" then
                        CoinData:Update(function(currentValue)
                            currentValue = currentValue + Value
                            return currentValue
                        end)
                    elseif Type == "Diamonds" then
                        DiamondData:Update(function(currentAmount)
                            currentAmount = currentAmount + Value
                            return currentAmount
                        end)
                    elseif Type == "Inventory" then
                        InventoryData:Update(function(currentDataTable)
                            if currentDataTable[Value] then
                                currentDataTable[Value] = currentDataTable[Value] + Amount
                            else
                                currentDataTable[Value] = Amount
                            end
                        end)
                    elseif Type == "Pets" then
                        PetData:Update(function(currentDataTable)
                            if currentDataTable[Value] then
                                currentDataTable[Value] = currentDataTable[Value] + Amount
                            else
                                currentDataTable[Value] = Amount
                            end
                        end)
                    end
                end
            end
        elseif typ == "Coins" then
            if (data >= QuestTable.Amount) and (not playerQuestData[QuestName]) then

            end
        elseif typ == "Diamonds" then
            if (data >= QuestTable.Amount) and (not playerQuestData[QuestName]) then

            end
        elseif typ =="Pets" then

        end
    end
end

Players.PlayerAdded:Connect(function(player)
    local InventoryData = Datastore2("Inventory"..DataNum.Value, player)
    local PetData = Datastore2("Pets"..DataNum.Value, player)
    local CoinData = Datastore2("Coins"..DataNum.Value, player)
    local DiamondData = Datastore2("Diamonds"..DataNum.Value,player)
    local MaxBagData = Datastore2("MaxBag"..DataNum.Value, player)

    local playerInventory = InventoryData:Get({})
    local playerPets = PetData:Get({})
    local PlayerCoins = CoinData:Get(0)
    local playerDiamonds = DiamondData:Get(0)
    local MaxBagSize = MaxBagData:Get(10)

    local playerDataFolder = Instance.new("Folder")
    playerDataFolder.Name = player.UserId
    playerDataFolder.Parent = replicatedData

    local Inventory = Instance.new("StringValue")
    Inventory.Name = "Inventory"
    Inventory.Parent = playerDataFolder
    Inventory.Value = HttpService:JSONEncode(playerInventory)

    local Pets = Instance.new("StringValue")
    Pets.Name = "Pets"
    Pets.Parent = playerDataFolder
    Pets.Value = HttpService:JSONEncode(playerPets)

    local lb = Instance.new("Folder")
    lb.Name = "leaderstats"
    lb.Parent = player

    local Coins = Instance.new("NumberValue")
    Coins.Name = "Coins"
    Coins.Parent = lb
    Coins.Value = PlayerCoins

    local Diamonds = Instance.new("NumberValue")
    Diamonds.Name = "Diamonds"
    Diamonds.Parent = lb
    Diamonds.Value = playerDiamonds

    DiamondData:OnUpdate(function(currentAmount)
        Diamonds.Value = currentAmount
        DataChanged:FireClient(player, "Diamonds", currentAmount)
    end)

    CoinData:OnUpdate(function(currentAmount)
        Coins.Value = currentAmount

        DataChanged:FireClient(player, "Coins", currentAmount)
    end)

    PetData:OnUpdate(function(DecodedData)
        Pets.Value = HttpService:JSONEncode(DecodedData)
        DataChanged:FireClient(player, "Pets", DecodedData)
    end)

    InventoryData:OnUpdate(function(DecodedData)
        Inventory.Value = HttpService:JSONEncode(DecodedData)
        checkQuests(player, "Inventory", DecodedData)
        DataChanged:FireClient(player, "Inventory", DecodedData)
    end)
    MaxBagData:OnUpdate(function(currentAmount)

    end)
end)

Players.PlayerRemoving:Connect(function(player)
    local playerData = replicatedData:FindFirstChild(player.UserId)
    local InventoryData = Datastore2("Inventory", player)
    local PetData = Datastore2("Pets", player)
    local CoinData = Datastore2("Coins", player)
    local DiamondData = Datastore2("Diamonds",player)

    InventoryData:Save()
    PetData:Save()
    CoinData:Save()
    DiamondData:Save()

    if (playerData) then
        playerData:Destroy()
    end
end)

Quest Module:

local replicatedStorage = game:GetService("ReplicatedStorage")
local Datastore2 = replicatedStorage:WaitForChild("Datastore2")
local DataNum = replicatedStorage:WaitForChild("DataNum")

local Quests = {
    ["Apple Lover"] = {
        Food = "Apple",
        Amount = 5,
        Reward = {
            Type = "Diamonds",
            Value = 5,
            Amount = nil,
        }

    },
    ["Banana Peels"] = {
        Food = "Banana",
        Amount = 10,
        Reward = {
            Type = "Inventory",
            Value = "Pineapple",
            Amount = 8,
        }
    },
}

return Quests

Answer this question