Checking if a user has gamepass

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

wait what. Somehow i retested it and its working now lol

1 Like

also I know this is really messy but its not working again.

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

local GAMEPASS_ID = 25131282
local MarketPlaceService = game:GetService("MarketplaceService")


game.Players.PlayerAdded:Connect(function(player)
	
	if MarketPlaceService:UserOwnsGamePassAsync(GAMEPASS_ID) then
		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 + 2
			game.Workspace.CurrentCamera.FieldOfView = game.Workspace.CurrentCamera.FieldOfView + .5
			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
			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)
end

A good way to debug code by yourself is to add print statements for each event/if statement to see where the code errors/stops. If you do not receive a studio error, it means your code is functioning properly (yet it might not function the way you want it to)

Can’t believe I forgot this, but you have to pass a UserId to the UserOwnsGamepassAsync, for example:

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(plr.UserId,gamepassId)
		-- Run gamepass stuff
	else
		-- Run normal stuff
	end
end)
1 Like

so that why it didnt work probally lol

Yes, probably. Try testing it out though, and report back if it has issues.

I tried it, I am not getting any errors in output.

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(plr.UserId,gamepassId)
		sizeEvent.OnServerEvent:Connect(function(plr, size)

			game.Workspace.Click:Play()
			plr:WaitForChild("leaderstats"):WaitForChild("Size").Value = plr:WaitForChild("leaderstats"):WaitForChild("Size").Value + 2
			game.Workspace.CurrentCamera.FieldOfView = game.Workspace.CurrentCamera.FieldOfView + .5
			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)

		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)
end
end)

plr:WaitForChild(“leaderstats”):WaitForChild(“Size”).Value = plr:WaitForChild(“leaderstats”):WaitForChild(“Size”).Value + 2

Correct Form Is plr:WaitForChild(“leaderstats”).Size.Value