WalkSpeed Death Error

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m making a 2X Speed gamepass for my obby.

  2. What is the issue? Include screenshots / videos if possible!
    When i die, the speed goes back to 24

  3. 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
2 Likes

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

i did everything you suggested and it didn’t work… could you send me a code snippet by any chance?

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.


i put the script in StarterCharacterScripts

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.

i put it in a local script

asdadsdadf (so i can reach the character minimum)

It’s meant to be a server-side script in ServerScriptService, thats why

ohhh alr lemme try that real quick

i put it into the script you said was correct and it still doesn’t work :confused:

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)

i already did that myself when i tested, and it didnt work…

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

2 Likes

Ohhhhh! This makes so much more sense thank you :slight_smile:

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)