✓ Accepted answer
You're treating PromptGamePassPurchaseFinished as if it were a function that returns a boolean, determining if the purchase was finished or not.
It is an event; you listen for it.
The listeners of this event get three arguments:
- the player who was prompted
- the game pass ID
- boolean if they purchased the pass or not.
But instead of connecting a function, we'll wait for it via the :Wait() method.
:Wait() returns what would be given as arguments to the listeners.
This code can also definitely be cleaned up. MP is a constant as the value won't change, so why declare the variable each time the part is touched?
local Players,
MarketplaceService =
game:GetService("Players"),
game:GetService("MarketplaceService");
local PASS_ID = 1087508;
local function protectedOwnsPass(client, passId)
local success, result = pcall(
MarketplaceService.UserOwnsGamePassAsync,
MarketplaceService,
client.UserId,
passId
);
if (success) then
return result;
end
warn("Error whilst web request:", result);
end
script.Parent.Touched:Connect(function(part)
local client = Players:GetPlayerFromCharacter(part.Parent);
if (client and protectedOwnsPass(client, PASS_ID)) then
return;
end
local humanoid = client.Character.Humanoid
local prevSpeed = humanoid.WalkSpeed
local bought = select(3, MarketplaceService.PromptGamePassPurchaseFinished:Wait());
if (not bought) then
humanoid:TakeDamage(humanoid.Health);
else
humanoid.WalkSpeed = prevSpeed;
end
end);
Some things that were used, but not explained
select selects a certain argument, and everything after it. So select(2, 'a', 'b', 'c', 'd') evaluates to 'b', 'c', 'd' since the second argument is selected, and everything else after it is selected also. If the first argument is a '#', it returns how many arguments were passed (excluding the first, which is what to select).
pcall (short for protected call), takes a function, and calls it in protected mode, so any runtime errors will not be propagated. Important for if your function makes web requests, or is just prone to error in general. The first result of this function is a boolean, true if the function was successfully called without errors, false otherwise. Everything after the first return value is what the function returns, but only if there wasn't an error. If there was an error, the first return value is false, and the second return value is the error message.
1 comment
Thank you, and sorry I took so long to accept the answer