Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to make developer product last until player leaves the server?

Asked by 7 years ago
Edited 7 years ago

Right now I have a dev. product set up to give a player +50 health upon purchasing a developer product. This works and all, and even continues working after the player resets.

However it lasts forever. I.e the player can leave and rejoin and still have 150 MaxHealth and 150 Health. I only want it to last until the player leaves the server or rejoins.

This is the script I have

local MarketplaceService = game:GetService("MarketplaceService")
local HealthID, PointsID = 89333927
local PurchaseHistory = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")

MarketplaceService.ProcessReceipt = function(receiptInfo) 

    for i, player in ipairs(game.Players:GetChildren()) do
        if player.userId == receiptInfo.PlayerId then
            if receiptInfo.ProductId == HealthID then
                player.Character.Humanoid.MaxHealth = player.Character.Humanoid.MaxHealth + 50
                player.Character.Humanoid.Health = player.Character.Humanoid.Health + 50
                 player.CharacterAdded:connect(function()
                player.Character.Humanoid.MaxHealth = player.Character.Humanoid.MaxHealth + 50
                player.Character.Humanoid.Health = player.Character.Humanoid.Health + 50
                 end)
            end
        end 
    end
    local playerProductKey = "player_" .. receiptInfo.PlayerId .. "_purchase_" .. receiptInfo.PurchaseId
    PurchaseHistory:IncrementAsync(playerProductKey, 1) 
    return Enum.ProductPurchaseDecision.PurchaseGranted     
end

I know it has something to do with the CharacterAdded. I don't know how to get around this. Help would be appreciated!

2 answers

Log in to vote
0
Answered by
Asceylos 562 Moderation Voter
7 years ago
Edited 7 years ago

By using the PlayerRemoving event.

local MarketplaceService = game:GetService("MarketplaceService")
local HealthID, PointsID = 89333927
local PurchaseHistory = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")

MarketplaceService.ProcessReceipt = function(receiptInfo) 
    for i, player in ipairs(game.Players:GetChildren()) do
        if player.userId == receiptInfo.PlayerId then
            if receiptInfo.ProductId == HealthID then
local a = false
                player.Character.Humanoid.MaxHealth = player.Character.Humanoid.MaxHealth + 50
                player.Character.Humanoid.Health = player.Character.Humanoid.Health + 50
                 player.CharacterAdded:connect(function()
if a == false then
                player.Character.Humanoid.MaxHealth = player.Character.Humanoid.MaxHealth + 50
                player.Character.Humanoid.Health = player.Character.Humanoid.Health + 50
end
                 end)
game.Players.PlayerRemoving:connect(function(plr)
if plr == player then a = true
end
end)
            end
        end 
    end
    local playerProductKey = "player_" .. receiptInfo.PlayerId .. "_purchase_" .. receiptInfo.PurchaseId
    PurchaseHistory:IncrementAsync(playerProductKey, 1) 
    return Enum.ProductPurchaseDecision.PurchaseGranted     
end

And using a boolean value. You don't need to edit anything, just try the script.

0
Thank you for your help again. killerthedemon 14 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

You would have to use a value in the player, put a value in the player called "HealthUp" and whenever they buy it, set the value to true when they buy the item and it will last till they leave the server, if you want it to save use data stores! :) Asceylos's answer is also right!

Answer this question