Argument 2 missing or nil

The problem I am having is that some vehicles will have to be purchased via gamepass
image

while the others(free) will not be purchasable but available to everyone
image

but the script i currently have errors out and says Argument 2 missing or nil

local SpawnVehicle = function(Player , Vehicle)
	if not MarketplaceService:UserOwnsGamePassAsync(Player.UserId , tonumber(Vehicle.Parent.Parent.Gamepass.Value)) then -- Detects if player has the gamepass
		MarketplaceService:PromptGamePassPurchase(Player , tonumber(Vehicle.Parent.Parent.Gamepass.Value))
		return
	end

	if not Vehicle.Parent.Parent.allowed.Value then -- detects if allowed value is false, if so stops script
		return
	end
	print(Player)
	print(tonumber(Vehicle.Parent.Parent.Gamepass.Value))
	local Character = Player.Character or Player.CharacterAdded:Wait()

	local PlayerFolder = Folder(Player.UserId , MainFolder)
	PlayerFolder:ClearAllChildren()

	local New = Vehicle:Clone()
	print("spawning vehicle")
	New:SetPrimaryPartCFrame(Character:WaitForChild("HumanoidRootPart").CFrame*CFrame.new(10,0,0))
	New.Parent = PlayerFolder
end

when this part of the script is commented out the free vehicles are spawnable

	--[[if not MarketplaceService:UserOwnsGamePassAsync(Player.UserId , tonumber(Vehicle.Parent.Parent.Gamepass.Value)) then -- Detects if player has the gamepass
		MarketplaceService:PromptGamePassPurchase(Player , tonumber(Vehicle.Parent.Parent.Gamepass.Value))
		return
	end]]

	if not Vehicle.Parent.Parent.allowed.Value then -- detects if allowed value is false, if so stops script
		return
	end

but when this part of the script is commented out the gamepass one is purchaseable

	if not MarketplaceService:UserOwnsGamePassAsync(Player.UserId , tonumber(Vehicle.Parent.Parent.Gamepass.Value)) then -- Detects if player has the gamepass
		MarketplaceService:PromptGamePassPurchase(Player , tonumber(Vehicle.Parent.Parent.Gamepass.Value))
		return
	end

	--[[if not Vehicle.Parent.Parent.allowed.Value then -- detects if allowed value is false, if so stops script
		return
	end]]
1 Like

do a check before this line

if not MarketplaceService:UserOwnsGamePassAsync(Player.UserId , tonumber(Vehicle.Parent.Parent.Gamepass.Value)) then -- Detects if player has the gamepass

Check if Vehicle.Parent.Parent.Gamepass.Value doesn’t equal nil.

if Vehicle.Parent.Parent.Gamepass.Value ~= nil then
    if not MarketplaceService:UserOwnsGamePassAsync(Player.UserId , tonumber(Vehicle.Parent.Parent.Gamepass.Value)) then -- Detects if player has the gamepass
    
    end
end

the error is caused by tonumber(Vehicle.Parent.Parent.Gamepass.Value) because you use this even when Vehicle.Parent.Parent.Gamepass.Value = nil and you cant use nil in tonumber()

1 Like