Checking if a user has gamepass

What I want to achieve: Check if the user has a gamepass and if so add a else statement to a function

thats basically it

1 Like

If you want to run something if they DON’T have a gamepass, you can try this

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local gamepassId = 0 -- Put gamepass ID here

Players.PlayerAdded:Connect(function(plr)
	if not MarketplaceService:UserOwnsGamePassAsync(gamepassId) -- Check if they don't own it
		-- Run code
	end
end)

This runs every time someone joins the game, but it works the same way with just the if statement.

I was gonna do something like

(if they got gamepass then)
My script i got

else:
other code

Ah. Should be similar then!

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local gamepassId = 0 -- Put gamepass ID here

Players.PlayerAdded:Connect(function(plr)
	if MarketplaceService:UserOwnsGamePassAsync(gamepassId)
		-- Run gamepass stuff
	else
		-- Run normal stuff
	end
end)
1 Like

alright thx ima try this, also would this word in a local and normal script?

Not sure, but if it does, it’s easily exploitable. You’re probably better off handling everything serverside (aside from visual changes)

all that would be handled in the local script would be a useless part of leaderstats basically

So would this be good? (this is my script inside server script service)

local rs = game:GetService('ReplicatedStorage')
local sizeEvent = rs:WaitForChild('SizeEvent')

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local gamepassId = 25131282 -- Put gamepass ID here

Players.PlayerAdded:Connect(function(plr)
	if MarketplaceService:UserOwnsGamePassAsync(gamepassId) then
	
		sizeEvent.OnServerEvent:Connect(function(plr, size)

			plr.Character.Humanoid.HeadScale.Value = plr.Character.Humanoid.HeadScale.Value + .1
			plr.Character.Humanoid.BodyDepthScale.Value = plr.Character.Humanoid.BodyDepthScale.Value + .1
			plr.Character.Humanoid.BodyWidthScale.Value = plr.Character.Humanoid.BodyWidthScale.Value + .1
			plr.Character.Humanoid.BodyHeightScale.Value = plr.Character.Humanoid.BodyHeightScale.Value + .1
		end)
		else
	sizeEvent.OnServerEvent:Connect(function(plr, size)

		plr.Character.Humanoid.HeadScale.Value = plr.Character.Humanoid.HeadScale.Value + .05
		plr.Character.Humanoid.BodyDepthScale.Value = plr.Character.Humanoid.BodyDepthScale.Value + .05
		plr.Character.Humanoid.BodyWidthScale.Value = plr.Character.Humanoid.BodyWidthScale.Value + .05
		plr.Character.Humanoid.BodyHeightScale.Value = plr.Character.Humanoid.BodyHeightScale.Value + .05
	end)
end
end)

this is prob so wrong cause its also not working incase you need to know this is the local script

local frame = script.Parent

local grow = frame.Grow

local grow_val = 1


local player = game.Players.LocalPlayer

local rs = game:GetService('ReplicatedStorage')
local sizeEvent = rs:WaitForChild('SizeEvent')


grow.MouseButton1Click:Connect(function()
	sizeEvent:FireServer(grow_val)
	game.Workspace.Click:Play()
	game.Players.LocalPlayer.leaderstats.Size.Value = game.Players.LocalPlayer.leaderstats.Size.Value + 1
	game.Workspace.CurrentCamera.FieldOfView = game.Workspace.CurrentCamera.FieldOfView + .25
end)

Alright, I did some testing and it appears it DOES work in LocalScripts.

Also, OnServerEvent returns the player who called it, and since you’re binding it to the same event, it’ll remain the same for all incoming remotes. You may be better off checking if they own the gamepass inside the OnServerEvent function.

my tiny brain cant handle that

Okay, let me try again.

When you connect a function the a RemoteEvent, binding another will not clear it. When a client fires a remote event, ALL of the bound functions will be run. And since you’re binding more when players join, the more times players join, the more events that will be fired.

Wait so would I need to put how adds onto leaderstats inside of that normal script?

Yes, if you update leaderstats from LocalScripts, it won’t update/save/show for other players.

Try This:

local GAMEPASS_ID = 0 // Type Something Here
local MarketPlaceService = game:GetService("MarketPlaceService")

game.Players.PlayerAdded:Connect(function(player)
        if MarketPlaceService:UserOwnsGamePassAsync(GAMEPASS_ID)
              -- Your Code Here
        else
             -- Also Code Here
   end
end)

Wait this is giving me the error.attempt to index nil with ‘WaitForChild’ in line 7?

local rs = game:GetService('ReplicatedStorage')
local sizeEvent = rs:WaitForChild('SizeEvent')

sizeEvent.OnServerEvent:Connect(function(plr, size)
	
	game.Workspace.Click:Play()
	game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Size").Value = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Size").Value + 1
	game.Workspace.CurrentCamera.FieldOfView = game.Workspace.CurrentCamera.FieldOfView + .25
	plr.Character.Humanoid.HeadScale.Value = plr.Character.Humanoid.HeadScale.Value + .05
	plr.Character.Humanoid.BodyDepthScale.Value = plr.Character.Humanoid.BodyDepthScale.Value + .05
	plr.Character.Humanoid.BodyWidthScale.Value = plr.Character.Humanoid.BodyWidthScale.Value + .05
	plr.Character.Humanoid.BodyHeightScale.Value = plr.Character.Humanoid.BodyHeightScale.Value + .05
end)

Local Player is nil. Your probably using this in a server script

I am. I am trying to put this to where it updates the leaderboards for size inside of server script.
I have in the localscript to connect to SizeEvent and for it to track if they clicked

Uhm… the first parameter is literally ‘plr’… replace game.Players.Localplayer with plr since it is defined in the event receiver sizeEvent.OnServerEvent:Connect(function(plr, size)

I did that

local rs = game:GetService('ReplicatedStorage')
local sizeEvent = rs:WaitForChild('SizeEvent')

sizeEvent.OnServerEvent:Connect(function(plr, size)
	
	game.Workspace.Click:Play()
	plr:WaitForChild("leaderstats"):WaitForChild("Size").Value = plr:WaitForChild("leaderstats"):WaitForChild("Size").Value + 1
	game.Workspace.CurrentCamera.FieldOfView = game.Workspace.CurrentCamera.FieldOfView + .25
	plr.Character.Humanoid.HeadScale.Value = plr.Character.Humanoid.HeadScale.Value + .05
	plr.Character.Humanoid.BodyDepthScale.Value = plr.Character.Humanoid.BodyDepthScale.Value + .05
	plr.Character.Humanoid.BodyWidthScale.Value = plr.Character.Humanoid.BodyWidthScale.Value + .05
	plr.Character.Humanoid.BodyHeightScale.Value = plr.Character.Humanoid.BodyHeightScale.Value + .05
end)

Now i get no error but nothing i happening at all when I click it

Do you receive a ‘click sound’? Also it would be smart to replace
plr:WaitForChild("leaderstats"):WaitForChild("Size").Value = plr:WaitForChild("leaderstats"):WaitForChild("Size").Value + 1
with plr:WaitForChild("leaderstats"):WaitForChild("Size").Value +=1