You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I’m making a 2X Speed gamepass for my obby.
What is the issue? Include screenshots / videos if possible!
When i die, the speed goes back to 24
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I asked ChatGPT
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local ProductID = 1102696315
local MarketplaceServ = game:GetService("MarketplaceService")
script.Parent.MouseButton1Click:Connect(function()
MarketplaceServ:PromptGamePassPurchase(game.Players.LocalPlayer, ProductID)
MarketplaceServ.PromptGamePassPurchaseFinished:Connect(function(player, id, bool)
if bool and id == ProductID and player == game.Players.LocalPlayer then
player.Character.Humanoid.WalkSpeed = 48
end
end)
end)
if MarketplaceServ:PlayerOwnsAsset(game.Players.LocalPlayer, ProductID) then
while true do
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 48
end
end
The issue here, the reason why its bugging is because your checking first thing whether or not the player has the gamepass, and you didnt include a wait() in the while loop which could lag it.
You could use the CharacterAdded event which will check when the player spawns in, or respawns
local ProductID = 1102696315
game.Players.PlayerAdded:Connect(function(player: Player)
player.CharacterAdded:Connect(function(character: Model)
if MarketplaceServ:UserOwnsGamePassAsync(player.UserId, ProductID) then
player.Character.Humanoid.WalkSpeed = 48
end
end)
end)
Put this somewhere, I’d usually put it as a separate script but you can do whatever.
Is it a server side script? If not, make it one
Also, putting it into StarterCharacterScripts doesnt really work, ServerScriptService works for scripts like those.
Oh, i’m an idiot sorry
forgot to defy MarketplaceServ
local ProductID = 1102696315
local MarketplaceServ = game:GetService("MarketplaceService")
game.Players.PlayerAdded:Connect(function(player: Player)
player.CharacterAdded:Connect(function(character: Model)
if MarketplaceServ:UserOwnsGamePassAsync(player.UserId, ProductID) then
player.Character.Humanoid.WalkSpeed = 48
end
end)
end)
That’s because it’s a test buy, and it only fires the PromptGamePassPurchaseFinished event, but it won’t be true for UserOwnsGamePassAsync because it checks your inventory (Test buys don’t put the gamepass in your inventory). If you were to buy this game pass then it would probably work because it’s in your inventory
Dont use the code you posted use @kofi_123recoveryacc’s code. The issue with his is that it doesn’t account for when the player buys it for the first time.
Here’s fixed version:
local MarketPlace = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local ProductID = 0000
local function GiveSpeed(Player:Player)
if not Player then return end
local OwnsGamepass = MarketPlace:UserOwnsGamePassAsync(Player.UserId, ProductID)
local Char = Player.Character
if not Char or not OwnsGamepass then return end
local Humanoid = Char:FindFirstChildOfClass("Humanoid")
if not Humanoid then return end
Humanoid.WalkSpeed = 48
end
MarketPlace.PromptGamePassPurchaseFinished:Connect(function(player: Instance, gamePassId: number, wasPurchased: boolean)
if player and gamePassId == ProductID and wasPurchased then
GiveSpeed(player)
end
end)
Players.PlayerAdded:Connect(function(player: Player)
player.CharacterAppearanceLoaded:Connect(function(character: Model)
GiveSpeed(player)
end)
end)