Hello! Basically I’m sending a table with 5-ish results inside the table, But whenever I try and print one of the following:
print(i[1]), print[i.Test] (same w/ v)
And it prints nil:
This is my server code:
local function QuestsData()
return {
Play3Maps = {
Test = false;
InProgress = false;
NameQuest = "Play 3 Map's!";
Description = "Play any 3 map's in game for a reward.";
RewardInCoins = nil;
RewardInXP = 100
}
}
end
And this is the client:
if ServerData ~= nil then
for i, v in pairs(ServerData.Play3Maps) do
print(i.Test)
I apologise I should’ve done more extensive testing as it doesn’t work as desired;
That makes it extremely hard-coded. As I would need to check the condition every single time. My goal would to be do them all automatically with a loop.
It prints nil because i is just an index and is not a table that contains values in it.
Try:
if ServerData ~= nil then
for i,v in pairs(ServerData.Play3Maps) do
print(ServerData.Play3Maps[i])
Like the previous responder has mentioned, using if i == "Test" then print(i) end or simply print(ServerData["Play3Maps"].Test) would work better in this scenario. I’m pretty sure you would have to set a data structure for your quests which isn’t quite hard, it is still unclear to me what your intention is right now.
So you’re implying I should hard-code this? I thought it was bad practice, Also it clutters the code heavily, And by the time the game is completed there will be way over 250 quests. (Which would in result be a pain to maintain and if something goes wrong it would take HOURSSSSS rewriting every single value)
I am not saying you should hard-code EVERY quest there is in the game. Let’s say your array of quests looks like this:
local quests = {
Play3Maps = {
Test = false;
InProgress = false;
NameQuest = "Play 3 Map's!";
Description = "Play any 3 map's in game for a reward.";
RewardInCoins = nil;
RewardInXP = 100
},
Play4Hours = {
Test = false;
InProgress = false;
NameQuest = "Play For 4 Hours!";
Description = "Play this game for 4 Hours for a reward!";
RewardInCoins = nil;
RewardInXP = 300
}
--and etc
}
return quests
The array can later be iterated when retrieving their data, like this:
local Quests = require(PathToQuestsHere)
for i,v in ipairs(Quests) do
print("Name: ",v.NameQuest)
print("Description: ",v.Description)
print("Reward In XP: ",v.RewardInXP)
end
This makes it way easier to add quests by just editing the module script instead of adding extra lines to the code. This is a common practice in many games when adding maps, pets, abilities, auras, basically any category that contains a list of common key data inside it.
It doesn’t JUST print, There are over 33 lines for every single quest, I want to just compact it to one time. Also it would kill performance for lower end devices and would use SOOOO much memory.
local function CreateQuest(ServerData)
if ServerData ~= nil then
if ServerData.Play3Maps["CodeName"] == "PLAY3MAPS" then
print(ServerData.Play3Maps["Test"])
local New = script.QuestTemplate:Clone()
New.Parent = script.Parent.Quests
New.Name = ServerData.Play3Maps["CodeName"]
New.NameTXT.Text = ServerData.Play3Maps["NameQuest"]
if ServerData.Play3Maps["RewardInCoins"] ~= nil then
print(ServerData.Play3Maps["RewardInCoins"])
New.Reward.Text = "Reward: "..ServerData.Play3Maps["RewardInCoins"].. " Coins"
elseif ServerData.Play3Maps["RewardInXP"] ~= nil then
print(ServerData.Play3Maps["RewardInXP"])
New.Reward.Text = "Reward: "..ServerData.Play3Maps["RewardInXP"].." XP"
end
end
end
end
Edit* I understand the module, i’m more talking about the client.
All the script does is create new instances to display the quests that are needed, I do not see how this can use up a lot of memory unless there’s something in your system that I am not aware of.
Wait im hella confused right now, I don’t want to change the server, The server-side is fine, And I don’t use modules I use a table in the server, That sends with a remote event.
Im going to rephrase what i meant. The table im sending is stored in a datastore, and when the player joins, it fires one event to the client with the datastore data of the table. If value is true then it says completed vise versa. I just want it to get the table, and for every result in the table it clones a ui with the corresponding values.