(still need help) Problem with inserting a table into tables

Im not able to insert a table into a existing quest table. I use functions todo all of these. this is according to my developer, but he doesn’t have dev forum access.

CLICK TO GET QUEST GIVER

local part = script.Parent
local click = part.ClickDetector
local questSystem = require(game:GetService("ServerScriptService"):WaitForChild("QuestSystem"))

click.MouseClick:Connect(function(plr)
    local questData = {
        id = plr.UserId;
        name = "test quest";
        reward = {
            rewardType = "badge";
            val = 638496704;
        };
        objectives = {
            ["1"] = {
                name = "collect 500 blue bricks";
                progress = 0;
                goal = 500;
            };
            ["2"] = {
                name = "kill 10 people";
                progress = 0;
                goal = 10;
            };
            ["3"] = {
                name = "collect 500 red bricks";
                progress = 0;
                goal = 500;
            }
        }
    }
    local thing = questSystem:RegisterQuest(plr,questData)
end)

QUEST FUNCTION (inside a module)

function module:RegisterQuest(plr,questData,overwrite) --creates a new quest from the provided quest data
	if quests[plr.UserId] == nil then quests[plr.UserId] = {} end
	if quests[plr.UserId] ~= nil and overwrite == false then
		module:OverwriteQuest(plr)
		print("quest already ongoing")
		return "Quest already ongoing!"
	end
	table.insert(quests,questData)
	print("recieved player: "..tostring(plr) or "nil")
	print("quests table: "..tostring(table.unpack(quests)) or "nil")
	for _,v in pairs(quests) do
		if v.id == questData.id then
			print("players quest table: "..tostring(quests.v) or "nil")
			return v
		end
	end
	error("unable to add and find player's quest table!")
	--localSound:FireClient(plr,questData.sound)
end

any help is appreciated. thanks. this is kinda urgent btw.

Maybe you should consider making the quest data a “highest” scope variable? (take the questData and put it outside the mouse click event)

Try adding commas ( , ) instead of semicols( ; ) at the end of the insider tables.

doesn’t change anything. its the same as ;

1 Like

he said it didnt work as well. thanks anyways!

1 Like

Just a tip for avoiding errors:

  • Don’t use string indexes for tables that are numbers, use numbers instead (for the questData.objectives table

It’s helpful to say what lines are giving errors or just what in general isn’t working. Or what the output is.

The problem is probably that your quest table is being used incorrectly. You have user id as keys (which are numbers) and insert quests (which have 1-n as keys, also numbers).

You probably either need two tables, or you mean to insert the questData into a table inside of quests.

To illustrate the point:

local quests = {}
-- Now we add some data for a player with UserId 3
local plr = somePlayer -- assume somePlayer is a player
local somePlayerData = {}
quests[plr.UserId] = somePlayerData
-- Now quests is {[3] = somePlayerData} aka {nil, nil, somePlayerData}

-- Now lets insert some questData
local questData = someQuestData -- assume this is some quest data
table.insert(quests, questData)
-- Now quests is {[1] = questData, [3] = somePlayerData}
-- aka {questData, nil, somePlayerData}

See how the list now has different data types? What probably should happen is the questData should be added to the somePlayerData, not to the quests table.

tl;dr:
You’re using the quests table wrong, you need to add the quest data to the player’s data, not to the quest table:

table.insert(quests[plr.UserId],questData)

instead of

table.insert(quests,questData)
2 Likes

Thank you so much! We are now able to continue our script.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.