Hey developers!
I am currently making a VIP subscribtion system that requires 2 scripts. One of them handles developer products for the whole game, and one of them handles the VIP system.
Here’s the code…
Developer Product Script:
local ProductFunctions = {
[1165573680] = function(Player) -- 1 year
game.ServerStorage.AddTime:Fire(Player, 60*60*24*7*4*12)
return true
end;
}
function GetPlayerFromId(id)
for i,v in pairs(game.Players:GetPlayers()) do
if v.userId == id then
return v
end
end
return nil
end
game:GetService("MarketplaceService").ProcessReceipt = function(ReceiptInfo)
local ProductId = ReceiptInfo.ProductId
local PlayerId = ReceiptInfo.PlayerId
local Player = GetPlayerFromId(PlayerId)
if ProductFunctions[ProductId] then
local Output = ProductFunctions[ProductId](Player)
print(Output) -- printing "true"
if Output then
return Enum.ProductPurchaseDecision.PurchaseGranted
else
return Enum.ProductPurchaseDecision.NotProcessedYet
end
end
end
And the VIP script:
--// THIS IS INSIDE OF A PLAYERADDED FUNCTION
--// NEITHER OF THE PRINTS ARE PRINTING
game.ServerStorage.AddTime.Event:Connect(function(Sender, Time)
print("Fired")
if Sender == Player then
print("Event")
if VIPData < os.time() then
VIPData = os.time() + Time
else
VIPData += Time
end
VIPDatastore:Set(VIPData)
Remotes.TickEndChanged:FireClient(Player, VIPData)
end
end)
An event can only be subscribed to by one other script at a time. When a second script subscribes to an already subscribed event, the first script will be unsubscribed.
So only the last player added will get the subscription. You have to move that outside the player added event.
game.ServerStorage.AddTime.Event:Connect(function(Player, Time)
print("Fired")
if VIPDatas[Player.UserId] < os.time() then
VIPDatas[Player.UserId] = os.time() + Time
else
VIPDatas[Player.UserId] += Time
end
local VIPDatastore = Datastore2("VIPMAIN", Player)
VIPDatastore:Set(VIPDatas[Player.UserId])
Remotes.TickEndChanged:FireClient(Player, VIPDatas[Player.UserId])
end)
That being outside of the playeradded event.
Still not working, not even the “Fired” is printing.
So far as I can tell everything posted seems like it should work. Is there more code that might get in the way of that connection ever being made?
game.ServerStorage.AddTime.Event:Connect(function(Player, Time)
print("Fired")
if VIPDatas[Player.UserId] < os.time() then
VIPDatas[Player.UserId] = os.time() + Time
else
VIPDatas[Player.UserId] += Time
end
local VIPDatastore = Datastore2("VIPMAIN", Player)
VIPDatastore:Set(VIPDatas[Player.UserId])
Remotes.TickEndChanged:FireClient(Player, VIPDatas[Player.UserId])
end)
print("connected")
Like this would most definitely print “connected” right? Other than the slight potential for some simple mistake like containing that connection in a function you forgot to fire, I can’t really see any reason why this shouldn’t work.
I ran a test removing anything in your code I don’t actually have a reference to and it worked flawlessly for me.
And just a little note that is far from important.
function GetPlayerFromId(id)
for i,v in pairs(game.Players:GetPlayers()) do
if v.userId == id then
return v
end
end
return nil
end
This function exists as a part of game.Players. Players | Roblox Creator Documentation Though realistically there is no difference in using that or the other (even states that in the documentation)