Script only working in studio & gamepass script not working

Im trying to do a simple script and when the player joins the game they would get 5 coins but with the vip gamepass they would get 10 but I have the gamepass and not getting 10 coins per join only 5 like the rest of the players. And even worse the script does not work in-game not even getting 5 coins just nothing. No errors no nothing.

One was fixed it gives 10 Coins now but it does not work in-game only in studio

You are not checking for players that join, only when the server starts. In studio you may join immediately when running, but in real game servers your code may be running before the first player joins (Which means even it worked for the first player, it wouldn’t for players that join after)

You could try using this code, also make sure that the gamepass ID is right

local amount = 5 --/// Amount you want to give player when joining
local doubleAmountGamepassID = 870927087 --/// The ID of the gamepass you want player to have for 2x amount

local function onPlayerJoin(player: Player)
	local ownsGamepass: boolean = game.MarketplaceService:PlayerOwnsAsset(player, doubleAmountGamepassID)
	if ownsGamepass == true then
		player.leaderstats.Coins.Value += amount * 2
	else
		player.leaderstats.Coins.Value += amount
    end
end

for _, player: Player in pairs(game.Players:GetPlayers()) do
	onPlayerJoin(player) --/// In case a player has joined before the event connection was made
end

game.Players.PlayerAdded:Connect(onPlayerJoin) --/// Connects to the function whenever a player joins

Hope it helps!

1 Like

Works in-game now but does not give twice the amount of coins

In addition to Pinguaradd, a note i will make is to localise your variables by putting local Infront so they are localised to said script and also because it’s just good practice.

Ngl i would just make a separate variable for the double amount so it’s more customisable in the future instead of having to times it or do an addition for a odd number if that is ever needed.

1 Like

If the asset id is right, maybe try using MarketplaceService:UserOwnsGamepassAsync() instead of MarketplaceService:PlayerOwnsAsset()

Replace the onPlayerJoin function for this

local function onPlayerJoin(player: Player)
	local ownsGamepass: boolean = game.MarketplaceService:UserOwnsGamePassAsync(player.UserId, doubleAmountGamepassID)
	if ownsGamepass == true then
		player.leaderstats.Coins.Value += amount * 2
	else
		player.leaderstats.Coins.Value += amount
	end
end

Let me know if it works

Now it gave me no coins and still no errors

That’s strange, did you replace the whole code or only the onPlayerJoin function? The whole script should look like this:

local amount = 5 --/// Amount you want to give player when joining
local doubleAmountGamepassID = 870927087 --/// The ID of the gamepass you want player to have for 2x amount

local function onPlayerJoin(player: Player)
	local ownsGamepass: boolean = game.MarketplaceService:UserOwnsGamePassAsync(player.UserId, doubleAmountGamepassID)
	if ownsGamepass == true then
		player.leaderstats.Coins.Value += amount * 2
	else
		player.leaderstats.Coins.Value += amount
	end
end

for _, player: Player in pairs(game.Players:GetPlayers()) do
	onPlayerJoin(player) --/// In case a player has joined before the event connection was made
end

game.Players.PlayerAdded:Connect(onPlayerJoin) --/// Connects to the function whenever a player joins

If it still doesn’t work, maybe the problem is in the leaderstats script. I’ve tested it with a simple leaderstats and it should be working
image

1 Like

Screenshot 2024-07-26 194818
Still doesnt work (I had 200 coins before it)

It actually works it gave me 10 coins in studio but when i join in-game it doesnt give me any coins

Ok nvm of everything I said, it just had delays before getting the cash it works

1 Like

You gotta use BadgeService not MarketplaceService. :wink: