Gamepass not working as its not giving the currency



Should be giving coins when purchased

game.Players.PlayerAdded:Connect(function(joining)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = joining

local coins = Instance.new("IntValue")
coins.Name = "coins"
coins.Value = 0
coins.Parent = leaderstats

local part = workspace.Part 
local MarketPlaceService = game:GetService("MarketplaceService")
local GamepassID = 748426653
part.Touched:Connect(function(hit)
	local char = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(char)
	MarketPlaceService:PromptGamePassPurchase(player, GamepassID)
	
	if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamepassID) then
		coins.Value = coins.Value +10000
	end

end)

end)

1 Like

Why do you use a seperate player variable?

Looking at the first and only argument you used in line on in the PlayerAdded event, the player param is called joining.
Just leave the other player variable away and use the joining variable in line 17 and line 19.

image
and what, changing this function to joining aswell as changing line 15?

Oh, I now get it. And I know why this script is breaking. The script triggers the .Touched event even when there is a non-player entity touching it, so you need to ensure that it is a player touching this part.
Do this in the .Touched event:

part.Touched:Connect(function(hit)
    local hum = hit.Parent:FindFirstChild(”Humanoid“)
    if hum then
        local player
        if (game:GetService(”Players”):GetPlayerFromCharacter(hit.Parent) ~= nil) then
            player = game:GetService(”Players”):GetPlayerFromCharacter(hit.Parent)
        end
    -- continue code
    end
end

This should get the player instance correctly, though I can‘t try out right now.


it is highlighted

You have an extra end on line 23

thanks although it doesnt work

I’m assuming the thing the player is purchasing is a gamepass.

I had an old script from a game I was working on. I changed it up a bit and it should work for your game. (You can paste this outside of the function where the player gets prompted to buy the gamepass.)

game:GetService("MarketplaceService")PromptGamePassPurchaseFinished:Connect(function(plrid, id, bought)
	if bought == true then
		local plr = nil
		for i, v in pairs(game.Players:GetChildren()) do
			if v.UserId == plrid then
				plr = v
				break
			end
		end
		local devids = {
			coins10000 = 748426653,
			coins1000 = nil --here is where you would put more gamepass ids if needed
		}
		if id == devids.coins10000 then
			plr.leaderstats.coins.Value += 10000
		elseif id == devids.coins1000 then
			--if you want you can make more options for the player
		end
	end
end)