Gamepass script is not working

Hello!

My script below is not working, obviously lol. Its goal is to give the player a speedboost when they own a certain gamepass. The script that checks when the user owns the gamepass upon joining is not working, whereas the part where it detects after the purchase if it is owned does work. I spawned into my game, and I created the gamepass so I should automatically have it right? But no I don’t, so I make a test purchase and it works, but then I reset my character and it doesn’t work again. Can anybody help me in figuring out what is wrong?
Thanks!

local players = game:GetService("Players")
local mps = game:GetService("MarketplaceService")
local gpid = 121242946
local speedmultiplier = 2

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		--part that is not working
		if mps:UserOwnsGamePassAsync(player.UserId, gpid) then
			char.Humanoid.WalkSpeed = char.Humanoid.WalkSpeed * speedmultiplier
		end
		--part that is working
		local gpconnection = mps.PromptGamePassPurchaseFinished:Connect(function(player, gamepassid, purchased)
			if gamepassid == gpid and purchased then
				char.Humanoid.WalkSpeed = char.Humanoid.WalkSpeed * speedmultiplier
			end
		end)
		
		player.CharacterRemoving:Connect(function(char)
			gpconnection:Disconnect()
		end)
	end)
end)

1 Like

try player.CharacterAppearanceLoaded instead of CharacterAdded, maybe

2 Likes

Something like this should do, unless I misunderstood what you’re trying to do.

-- Define the gamepass ID and the multiplier for the walkspeed
local GAMEPASS_ID = 123456 -- Replace with your gamepass ID
local WALKSPEED_MULTIPLIER = 2

-- Define a function that sets the player's walkspeed based on whether they own the gamepass
local function setWalkspeed(player)
    local hasGamepass = game:GetService("MarketplaceService"):HasGamePassAsync(player.UserId, GAMEPASS_ID)
    if hasGamepass then
        player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed * WALKSPEED_MULTIPLIER
    end
end

-- Connect to the PlayerAdded event to check the player's gamepass ownership when they join the game
game.Players.PlayerAdded:Connect(function(player)
    setWalkspeed(player)
end)

-- Connect to the MarketplaceService event to check the player's gamepass ownership when it changes
game:GetService("MarketplaceService").PlayerHasPassChanged:Connect(function(player, gamepassId, hasPass)
    if gamepassId == GAMEPASS_ID then
        setWalkspeed(player)
    end
end)


2 Likes

No such thing as a PlayerHasPassedChanged event

1 Like

Why are you using ChatGPT to help solve someone’s problem lmfao

1 Like

for the fun i guess, and if it helps then it helps

1 Like

true
//////
//////
//////
//////

Did you create a “Gamepass” or a “Developer Product”

1 Like

if you’re not willing to help then don’t even bother to reply goofy lmfao

@Yawyawalpaca333 try and see if this works, if not then reply back to me

I also don’t understand why you’re disconnecting PromptGamePassPurchaseFinished when a player’s Character gets removed, the Event also fires when the player dies. So when the player dies and decides to purchase your gamepasses they wouldn’t even receive the reward

local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local gamepassID = 121242946
local speedMultiplier = 2

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, id, wasPurchased)
	if gamepassID == id and wasPurchased then
		local char = player.Character
		local humanoid = char:FindFirstChildOfClass("Humanoid")
		
		if char and humanoid then
			humanoid.WalkSpeed *= speedMultiplier
		end
	end
end)

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		-- We wrap it around a pcall
		-- to ensure it doesn't break
		-- our code when it fails
		local success, hasPass = pcall(function()
			return MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassID)
		end)
		
		if success then
			if hasPass then
				local humanoid = char:FindFirstChildOfClass("Humanoid")
				
				if humanoid then
					-- This is the same as doing
					-- Humanoid.WalkSpeed = Humanoid.WalkSpeed * speedMultiplier
					humanoid.WalkSpeed *= speedMultiplier
				end
			end
		else
			print("Failed checking if player has gamepass")
		end
	end)
end)
1 Like

This is not working. It’s doing the same thing as before. Here is a video of it in action, hopefully it embeds.
robloxapp-20230616-2016352.wmv (4.3 MB)

edit: Can someone also tell me how to send videos to where people don’t have to download stuff.

Also sorry for the late response I had a bunch of stuff come up.

1 Like

Use: Gyazo or Streamable because they are both online services that allow users to easily capture and share screenshots, images, and videos…

2 Likes

buying a gamepass:

local MarketplaceService = game:GetService("MarketplaceService")

local passID = 0000000  -- Change this to your Pass ID

-- Function to handle a completed prompt and purchase
local function onPromptPurchaseFinished(player, purchasedPassID, purchaseSuccess)
	if purchaseSuccess and purchasedPassID == passID then
		print(player.Name .. " purchased the Pass with ID " .. passID)
		-- Assign this player the ability or bonus related to the Pass
	end
end

-- Connect "PromptGamePassPurchaseFinished" events to the function
MarketplaceService.PromptGamePassPurchaseFinished:Connect(onPromptPurchaseFinished)

joining the game:

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local passID = 0000000  -- Change this to your Pass ID

local function onPlayerAdded(player)
	local hasPass = false

	-- Check if the player already owns the Pass
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, passID)
	end)

	-- If there's an error, issue a warning and exit the function
	if not success then
		warn("Error while checking if player has pass: " .. tostring(message))
		return
	end

	if hasPass then
		print(player.Name .. " owns the Pass with ID " .. passID)
		-- Assign this player the ability or bonus related to the Pass
	end
end

-- Connect "PlayerAdded" events to the function
Players.PlayerAdded:Connect(onPlayerAdded)

Both the scripts are serversided, while both
scripts are from the Roblox Document page.

1 Like

This is still not working, this time the speed is not increased for either of the purchase instances, whereas before it worked for when it got purchased but not when rejoined. And yes I assigned the walkspeed code with this code so that it should’ve worked. I typically don’t like the roblox default scripts like these as they tend to have weird functions.