i need to give my player Coins and Gems from 1 twitter code. here is the twitter code part:
local TwitterCodes = { Testy = {"Coins", 100}, -- Code 1 Test = {"Gems", 10000}, Testuru = {"Coins", 100,},-- Code 2 }
and here is the full twitter code script in case you need it:
--[ SERVICES ]-- 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
Hello!
You can do this by using a nested table and a loop. If you're interested in reading more about iterating over tables, check out the developer page about tables.
In the TwitterCodes
table, you can set each code to a nested table which contains the names of the leaderboard values you want to set. For example:
local TwitterCodes = { TestCode1 = { Coins = 100 }, TestCode2 = { Gems = 100 }, TestCode3 = { Coins = 1000, Gems = 100 } }
As seen in this example, you can set a code to only have one type that it redeems, or multiple. All you have to do is place the leaderboard name in the nested table. You can add as many as you want.
Then in your code, you can loop over the nested table to set the leaderboard values, like this:
-- Redeems Code. for LeaderboardName, AmountToGive in pairs(TwitterCodes[Code]) do -- Loops through the nested table which contains the names and amounts to give local LeaderboardValue = Player.leaderstats[LeaderboardName] LeaderboardValue.Value = LeaderboardValue.Value + AmountToGive end
Here is your code, with the updated nested loop:
--[ SERVICES ]-- local DataStoreService = game:GetService("DataStoreService") --[ TWITTER CODES ]-- local TwitterCodes = { TestCode1 = { Coins = 100 }, TestCode2 = { Gems = 100 }, TestCode3 = { Coins = 1000, Gems = 100 } } --[ 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. for LeaderboardName, AmountToGive in pairs(TwitterCodes[Code]) do -- Loops through the nested table which contains the names and amounts to give local LeaderboardValue = Player.leaderstats[LeaderboardName] LeaderboardValue.Value = LeaderboardValue.Value + AmountToGive end 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