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)
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.