Gamepass Detecting not working

I am trying to make night vision goggles a gamepass. But I tried making a script to detect if the player owns the game pass and let them use the goggles but it does not work. Here is the script:

local uis = game:GetService("UserInputService")


local char = script.Parent


local equipRE = game.ReplicatedStorage:WaitForChild("NVGsToggled")


local defaultAmbient = game.Lighting.OutdoorAmbient


local GamepassID = 17814605
game.Players.PlayerAdded:Connect(function(player)
	if game:GetService("MarketplaceService"):UserOwnsGamePassASync(player.UserId, GamepassID) then
		print("Owns gamepass")
		uis.InputBegan:Connect(function(input, processed)


			if processed then return end


			if input.KeyCode == Enum.KeyCode.K then


				if not char:FindFirstChild("NVGs") then

					equipRE:FireServer(true)
					game.Workspace["Night Vision Goggles Sound Effect"]:Play()


					game.Lighting.OutdoorAmbient = Color3.fromRGB(255, 255, 255)
					game.Lighting.ColorCorrection.Enabled = true

				else

					equipRE:FireServer(false)


					game.Lighting.OutdoorAmbient = defaultAmbient
					game.Lighting.ColorCorrection.Enabled = false
				end
			end
		end)
	end
end)

Is this a Localscript? If so, it’s likely because your player is added before the localscript has a chance to make the PlayerAdded event, you can get the LocalPlayer via game:GetService("Players").LocalPlayer and use that to check if the player has a gamepass

local uis = game:GetService("UserInputService")
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local Lighting = game:GetService("Lighting")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local char = script.Parent

local equipRE = ReplicatedStorage:WaitForChild("NVGsToggled")

local defaultAmbient = Lighting.OutdoorAmbient

local GamepassID = 17814605

local player = Players.LocalPlayer

if MarketplaceService:UserOwnsGamePassAsync(player.UserId, GamepassID) then
	print("Owns gamepass")
	uis.InputBegan:Connect(function(input, processed)
		if processed then return end

		if input.KeyCode == Enum.KeyCode.K then
			if not char:FindFirstChild("NVGs") then
				equipRE:FireServer(true)
				workspace["Night Vision Goggles Sound Effect"]:Play()

				Lighting.OutdoorAmbient = Color3.fromRGB(255, 255, 255)
				Lighting.ColorCorrection.Enabled = true
			else
				equipRE:FireServer(false)
				Lighting.OutdoorAmbient = defaultAmbient
				Lighting.ColorCorrection.Enabled = false
			end
		end
	end)
end

Also probably related, you wrote Async as ASync,

its a LocalScript In starterCharacterScripts thats prob why

Yeah now it prints the Print statement but if I press K it wont activate the goggles?

Could it be this if statement?

if not char:FindFirstChild("NVGs") then

If you dont have the goggles it will probably do the stuff for if you have the goggles? Maybe remove the not?

I removed not but it gives me an error: NVGs is not a valid member of Model “Workspace.ForgottenDogYT”

Is this coming from the RemoteEvent? Maybe you need to make a check there if the player has NVGs or not?

there is the remote event but the goggles hat is inside serverscriptservice inside a script which gives the hat

Which script is it erroring in?

The ServerScriptService one. The one which Equips the hat. This is the script:

game.ReplicatedStorage.NVGsToggled.OnServerEvent:Connect(function(plr, equipping)
	
	
	local char = plr.Character
	local humanoid = char:WaitForChild("Humanoid")
	
	
	if equipping then
		
		humanoid:AddAccessory(script.NVGs:Clone())
		
		
	else
		
		char.NVGs:Destroy()
	end
end)

Okay yea I think the not has to stay, maybe it’s because you need to tweak the OutdoorAmbient Color3?

1 Like

Strange. I added not again and it worked. Thank you

1 Like