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

Prompt purchase finished running multiple times?

Asked by
NUKlTO 0
2 years ago
Edited 2 years ago

Greetings, I have a script that prints "Player has purchased the asset" and fires a RemoteEvent once a player buys a clothing asset, however if the player clicks multiple times on the ClickDetector that opens the Purchase Prompt or cancels and opens the Purchase Prompt multiple times before purchasing the asset then it causes "Player has purchased the asset" and the RemoteEvent to be fired multiple times.

Thanks in advance!

script.Parent.MouseClick:connect(function(player)
    local ID = script.Parent.Parent.ID.Value
    local MarketPlaceService = game:GetService("MarketplaceService")
    local ServerStorage = game:GetService("ServerStorage")

    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local RemoteEvent = ReplicatedStorage:WaitForChild("PurchaseMessage") 

    local AssetType = "shirt"

    if MarketPlaceService:PlayerOwnsAsset(player, ID) == false then
            MarketPlaceService:PromptPurchase(player, ID)
        MarketPlaceService.PromptPurchaseFinished:connect(function(player, ID, IsPurchased)
            if IsPurchased then
                    print("Player has purchased the asset")
                    local playername = player.DisplayName
                RemoteEvent:FireAllClients(playername, AssetType)
            end
        end)
    else
        local GUI = ServerStorage.OwnsClothingAsset:Clone()
        GUI.Parent = player.PlayerGui
        wait(1.5)
        GUI:Destroy()
    end
end)
0
Is it a Developer Product or a Gamepass or a Shirt? MattVSNNL 620 — 2y
0
It's a shirt NUKlTO 0 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Solution

I think a way to do this is to set the MaxActivationDistance on the ClickDetector to 0 on the client.

So create a RemoteEvent and name it to something and put it in ReplicatedStorage. Also create LocalScript and place it in StarterPlayerScripts

Fix

-- Server Script
script.Parent.MouseClick:connect(function(player)
    game.ReplicatedStorage.SomethingEvent1:FireClient(player, script.Parent)
    local ID = script.Parent.Parent.ID.Value
    local MarketPlaceService = game:GetService("MarketplaceService")
    local ServerStorage = game:GetService("ServerStorage")

    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local RemoteEvent = ReplicatedStorage:WaitForChild("PurchaseMessage") 

    local AssetType = "shirt"

    if MarketPlaceService:PlayerOwnsAsset(player, ID) == false then
            MarketPlaceService:PromptPurchase(player, ID)
        MarketPlaceService.PromptPurchaseFinished:connect(function(player, ID, IsPurchased)
            if IsPurchased then
                    print("Player has purchased the asset")
                    local playername = player.DisplayName
                RemoteEvent:FireAllClients(playername, AssetType)
            end
        end)
    else
        local GUI = ServerStorage.OwnsClothingAsset:Clone()
        GUI.Parent = player.PlayerGui
        game.Debris:AddItem(GUI, 1.5)
    end
    game.ReplicatedStorage.SomethingEvent1:FireClient(player, script.Parent)
end)

-- Local Script

game.ReplicatedStorage.SomethingEvent1.OnClientEvent:Connect(function(ClickDetector)
    if ClickDetector.MaxActivationDistance == 0 then
        ClickDetector.MaxActivationDistance == 32
    else
        ClickDetector.MaxActivationDistance == 0
    end
end)

It's better to use debris service instead of waiting and then destroying since it'll yield the lua thread meaning it'll pause the script until that timer has been reached.

Ad

Answer this question