Help with Tables

When I’m making a table and trying to retrieve something from it, it keeps passing an error like when I try to use it with subscriptions.

Example of table;

local Table = {
 ["EXP-1111111"] = "VIP"
}

And when my script tries to pull it from the table it says ‘Invalid Subscription ID’; this is the script.

local P_Service : Players = game:GetService("Players")

P_Service.PlayerAdded:Connect(function(plr)
	
	checkSubscriptionStatus(plr, Subscription_Ids)
	
end)

Any fix or does tables not work like this; the Code Assist provided the template thingy for the table.

Not sure why you’d need to type check P_Service, since game:GetService("Players") guarantees that it returns that service.
Can I see the code for checkSubscriptionStatus()?

I’m not sure either I just like how it’s like that and sure hold on.

local function checkSubscriptionStatus(player : Player, subscriptionId : string)
	
	local success, response = pcall(function()
		
		return MS_Service:GetUserSubscriptionStatusAsync(player, subscriptionId)
		
	end)

	if not success then
		
		warn(`Error while checking if player has subscription: {response}`)
		
		return
			
	end

	if response.IsSubscribed then
		
		grantAward(player, subscriptionId)
		
	else
		
		revokeAwardIfGranted(player, subscriptionId)
		
	end
	
end

Ok, well, I should’ve probably asked this first: Is the ID for the subscription correct? If so, how do you define Subscription_Ids?

Yes I checked the ID and it’s correct, and it’s defined like this;

local Subscription_Ids = {
	["EXP-5607828639684755692"] = "VIP"
}

I also edited the script to this;

P_Service.PlayerAdded:Connect(function(plr)
	
	for _, array in pairs(Subscription_Ids) do
		
		checkSubscriptionStatus(plr, array)
		
	end
	
end)

It still gives off the Invalid Subscription ID error though.

Ok well there’s your problem.
I’m not sure how you understand tables and for loops, but as an example, here’s what a basic for loop would do with that table:

for i, v in pairs(Subscription_Ids) do
	print(i) -- EXP-5607828639684755692
	print(v) -- VIP
end

So in your case, you’d want to use the first variable in the for loop to get the ID of the subscription:

for id, _ in pairs(Subscription_Ids) do
	checkSubscriptionStatus(plr, id)
end
1 Like

Hm, alright but what was the point in Code Assist giving this then;

["EXP-5607828639684755692"] = "VIP"

And when I also edited the code before the for _, array thing I had the table like this;

local Table = {
 "EXP-11111"
}

And it still gave off the error when I did it without the for loop and even with it.

I don’t really understand. I’m not some AI whisperer. It gives you code depending on what you’ve prompted it. But if you wanted the table to be a basic array, you can write it as:

local Table = {"EXP-5607828639684755692"}

and then for the for loop:

for _, id in pairs(Subscription_Ids) do
	checkSubscriptionStatus(plr, id)
end
1 Like

I had it like that and it still errored.

Can I see both the table and the for loop? Those that I provided shouldn’t error.

local Subscription_IDs = {
 "EXP-5607828639684755692"
}
for _, array in pairs(Subscription_IDs) do
    checkSubscriptionStatus(plr, array)
end