Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

how do i make multiple Values for one Table Variable?

basically i have a table of twitter codes but i want one of them to give me 2 leaderstat values: Coins and Gems. I want them to be on the same line. im sure this is an easy fix? code:

local TwitterCodes = {
	Testificate = {"Coins", 100}, -- Code 1
	Test = {"Coins", 10000},-- Code 2
	abcdef = {"Coins", 10000},-- Code 3
}

2 answers

If I understand you correctly, you want a table structure like:

TwitterCodes ?Testificate ??# of coins ??# of gems ?Test ??# of coins ??# of gems ?abcdef ?# of coins ?# of gems

but you want each element on one line. This can be done:

local TwitterCodes = {
    Testificate = {Coins = #, Gems = #}, -- Code 1
    Test = {Coins = #, Gems = #},-- Code 2
    abcdef = {Coins = #, Gems = #},-- Code 3
}

I think your main issue is you misunderstand how tables work.

A table is a list of entries, having an index and a value

index value
1 "value1"
2 "value2"

an index can be a string or a number. A value can be anything, including another table.

Lets take a look at how they are constructed.

First of all a string indexed table

local strTable = {
	strIndex = "value",
	strIndex2 = "value2"
}

local strTable2 = {
	["strIndex"] = "value",
	["strIndex2"] = "value2"
}
index value
"strIndex" "value"
"strIndex2" "value2"

strTable and strTable2 are exactly the same, just two different ways of making string indexes

these can also be done on one line

local strTable = {strIndex = "value", strIndex2 = "value2"}

local strTable2 = {["strIndex"] = "value", ["strIndex2"] = "value2"}

and you can have a value be a table

local strTable = {table = {subtable = "value"}}

local strTable2 = {["table"] = {["subtable"] = "value"}}

if there is any confusion ask and I'm happy to elaborate.

#Edit

I saw the code you posted and I think I understand. You want the first value to be the name of the second value. I don't see why this is necessary, keep the table that I showed you and just do:

-- Redeems Code.
local AmountToGivePlayer = TwitterCodes[Code].Coins
local LeaderboardValue = Player.leaderstats.Coins

instead.

7 comments

0

I understand now, thank you so much!

0

local TwitterCodes = { Testificate = {Coins = #, Gems = #}, -- Code 1 Test = {Coins = #, Gems = #},-- Code 2 abcdef = {Coins = #, Gems = #},-- Code 3 } doesnt seem to be working ;/

0

I saw the code you posted and edited my answer.

0

if i just put coins on Local AmountToGivePlayer = TwitterCodes[Code].Coins? wont it only give me coins?

0

@TSSRafa do the same for gems, just replace coins with gems

0

could you explain a bit further i'm having trouble understanding exactly what you want me to do.

0

Nevermind! i figured it out lol

@benbehop this is the whole script

local DataStoreService = game:GetService("DataStoreService")

--[ TWITTER CODES ]--

local TwitterCodes = {
	Testy = {"Coins", 100}, -- Code 1
	Test = {"Gems", 10000},
	Testuru = {"Coins", 100,},-- Code 2
}

--[ FUNCTION ]--

game.ReplicatedStorage.RedeemCode.OnServerInvoke = function(Player, Code)

	local TwitterCodeStore = DataStoreService:GetDataStore("TwitterCodeStore".. Code) -- Getting the data store.
	local PlayerRedeemedCode = TwitterCodeStore:GetAsync(Player.UserId) -- Seeing if player redeemed code.

	if TwitterCodes[Code] and not PlayerRedeemedCode then -- Checking if its a code, and not redeemed yet.

		-- Redeems Code.
		local LeaderboardValueName = TwitterCodes[Code][1]
		local AmountToGivePlayer = TwitterCodes[Code][2]
		local LeaderboardValue = Player.leaderstats[LeaderboardValueName]

		LeaderboardValue.Value = LeaderboardValue.Value + AmountToGivePlayer

		TwitterCodeStore:SetAsync(Player.UserId, true) -- Settings Datastore to true.

		return true -- Tells client successfully redeemed.
	else
		-- Tells Player Can't Redeem.
		return false -- Tells client it was not redeemed.
	end

end

This discussion is preserved as it was. New answers, comments, and votes are closed.